Skip to main content
Back to problems
#599
Easy Algorithms

Minimum index sum of two lists

Array Hash Table String
59.5% acceptance
Jan 13, 2026
2108
416
Given two arrays of strings list1 and list2, find the common strings with the least index sum. A common string is a string that appeared in both list1 and list2. A common string with the least index sum is a common string such that if it appeared at list1[i] and list2[j] then i + j should be the minimum value among all the other common strings. Return all the common strings with the least index sum. Return the answer in any order.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn find_restaurant(list1: Vec<String>, list2: Vec<String>) -> Vec<String> {
    use std::collections::HashMap;
    let map: HashMap<&str, usize> = list1.iter().enumerate().map(|(i,s)| (s.as_str(), i)).collect();
    let mut best = usize::MAX;
    let mut result = Vec::new();
    for (j, s) in list2.iter().enumerate() {
      if let Some(&i) = map.get(s.as_str()) {
        let sum = i + j;
        if sum < best { best = sum; result = vec![s.clone()]; }
        else if sum == best { result.push(s.clone()); }
      }
    }
    result
  }
}