Minimum Index Sum of Two Lists - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"]
Output: ["Shogun"]
💡 Note: "Shogun" appears at index 0 in list1 and index 3 in list2, giving sum 0+3=3. This is the only common string with the minimum index sum.
Example 2 — Multiple Results
$ Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"]
Output: ["Shogun"]
💡 Note: "Shogun" has sum 0+1=1, "KFC" has sum 3+0=3, "Burger King" has sum 2+2=4. "Shogun" has the minimum sum of 1.
Example 3 — Single Match
$ Input: list1 = ["happy","sad","good"], list2 = ["sad","happy","good"]
Output: ["sad","happy"]
💡 Note: "happy": 0+1=1, "sad": 1+0=1, "good": 2+2=4. Both "sad" and "happy" have minimum sum 1.

Constraints

  • 1 ≤ list1.length, list2.length ≤ 1000
  • 1 ≤ list1[i].length, list2[i].length ≤ 30
  • list1[i] and list2[i] consist of spaces ' ' and English letters.
  • All the strings of list1 are unique.
  • All the strings of list2 are unique.

Visualization

Tap to expand
Minimum Index Sum of Two Lists INPUT list1: 0: "Shogun" 1: "Tapioca Exp" 2: "Burger King" 3: "KFC" list2: 0: "Piatti" 1: "The Grill..." 2: "Hungry..." 3: "Shogun" Goal: Find common strings with minimum index sum (i + j) Hash Map from list1: "Shogun" --> 0 "Tapioca Express" --> 1 "Burger King" --> 2 "KFC" --> 3 ALGORITHM STEPS 1 Build Hash Map Store list1 strings with indices 2 Iterate list2 Check if string exists in map 3 Calculate Index Sum sum = map[str] + j 4 Track Minimum Update result if sum smaller Index Sum Calculation: String i j Sum "Piatti" - 0 N/A "The Grill" - 1 N/A "Hungry H" - 2 N/A "Shogun" 0 3 3 Min Sum = 3 FINAL RESULT Common String Found: "Shogun" Index Details: list1[0] = "Shogun" list2[3] = "Shogun" Index Sum: 0 + 3 = 3 (minimum) Output: ["Shogun"] OK - Only common string! Key Insight: Using a Hash Map gives O(1) lookup time. Store list1 strings with their indices, then iterate through list2. For each match, calculate i + j and track the minimum. Time: O(n + m), Space: O(n) where n, m are list lengths. TutorialsPoint - Minimum Index Sum of Two Lists | Hash Map Optimization
Asked in
Yelp 15 Amazon 12 Google 8 Facebook 6
89.0K Views
Medium Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen