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
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force way to solve this is to check every possible combination of words from the two lists. We are looking for common words and tracking which ones appear earliest in both lists. Then we pick the common word combination that has the lowest combined early position.
Here's how the algorithm would work step-by-step:
def find_restaurant(list1, list2):
minimum_index_sum = float('inf')
result = []
# Iterate through the first list
for first_list_index in range(len(list1)):
for second_list_index in range(len(list2)):
# Check for common strings
if list1[first_list_index] == list2[second_list_index]:
current_index_sum = first_list_index + second_list_index
# Compare index sum to minimum and update
if current_index_sum < minimum_index_sum:
minimum_index_sum = current_index_sum
result = [list1[first_list_index]]
# Accumulate matching restaurant names
elif current_index_sum == minimum_index_sum:
result.append(list1[first_list_index])
return result
The key to solving this problem efficiently is to remember the location of each word in the first list. Then, when we look at the second list, we can quickly find out if a word is also in the first list and where it was located. This avoids checking every pair of words.
Here's how the algorithm would work step-by-step:
def findRestaurant(list1, list2): word_index_map = {}
for index, word in enumerate(list1):
word_index_map[word] = index
minimum_index_sum = float('inf')
result = []
for index2, word in enumerate(list2):
if word in word_index_map:
#We found a common word. Now calculate the index sum
index_sum = word_index_map[word] + index2
if index_sum < minimum_index_sum:
#New smallest sum, reset the result
minimum_index_sum = index_sum
result = [word]
elif index_sum == minimum_index_sum:
#Another word with the same smallest sum
result.append(word)
return result
Case | How to Handle |
---|---|
Both lists are empty | Return an empty list since there are no common interests. |
One list is empty, the other is not | Return an empty list since there are no common interests. |
One or both lists contain null or empty strings | Treat null/empty strings as valid strings for comparison purposes or filter them out before processing. |
Lists have no common interests | Return an empty list if no common interests are found after iterating through both lists. |
Lists have a single common interest at the very beginning | Correctly identifies and returns the single common interest with index sum 0. |
Lists have a single common interest at the very end | Iterate through the entire lists to find the common interest at the end, resulting in potentially a large index sum. |
Lists have multiple common interests with the same minimum index sum | Collect all common interests with the minimum index sum and return them as a list. |
Very large lists that may cause performance issues | Using a HashMap for one list allows for O(n+m) time complexity, which is more efficient than O(n*m). |