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.
Example 1:
Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["Piatti","The Grill at Torrey Pines","Hungry Hunter Steakhouse","Shogun"] Output: ["Shogun"] Explanation: The only common string is "Shogun".
Example 2:
Input: list1 = ["Shogun","Tapioca Express","Burger King","KFC"], list2 = ["KFC","Shogun","Burger King"] Output: ["Shogun"] Explanation: The common string with the least index sum is "Shogun" with index sum = (0 + 1) = 1.
Example 3:
Input: list1 = ["happy","sad","good"], list2 = ["sad","happy","good"] Output: ["sad","happy"] Explanation: There are three common strings: "happy" with index sum = (0 + 1) = 1. "sad" with index sum = (1 + 0) = 1. "good" with index sum = (2 + 2) = 4. The strings with the least index sum are "sad" and "happy".
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.list1
are unique.list2
are unique.list1
and list2
.This problem asks to find common strings between two lists and return those with the minimum sum of indices. The most efficient approach leverages a hash table (or dictionary in Python) for fast lookups.
Algorithm:
Create a Hash Table: A hash table (d
) is created from list2
. The keys are the strings in list2
, and the values are their respective indices. This allows for O(1) average-case lookup time.
Iterate and Compare: The code iterates through list1
. For each string s
in list1
:
s
exists as a key in the hash table d
. If it does, this means s
is a common string.i
(from list1
) and j
(retrieved from d
) are summed.mi
(minimum index sum) keeps track of the lowest sum encountered so far.
i + j
is less than mi
, it means a new minimum sum is found. The ans
(answer array) is reset to contain only s
, and mi
is updated.i + j
equals mi
, it means another string has the same minimum index sum; s
is appended to ans
.Return Result: Finally, the function returns the ans
array containing all common strings with the minimum index sum.
Time Complexity Analysis:
list2
takes O(n2) time, where n2 is the length of list2
.list1
takes O(n1) time, where n1 is the length of list1
. The hash table lookup inside the loop is O(1) on average.Space Complexity Analysis:
d
stores at most n2
entries (strings and their indices).ans
array stores at most min(n1, n2)
strings (the number of common strings).list2
are unique and present in list1
), or potentially lower if there are many duplicates.The provided code in Python, Java, C++, Go, TypeScript, and Rust all implement this algorithm effectively, with minor syntactic differences depending on the language's features (e.g., use of HashMap
vs. built-in dictionaries). The core logic remains consistent across all the implementations.