Intuition
We can store all the words from the first list into a hashtable along with their indices as the keys. Then iterate over the other list, and the word in list2
is already seen in the list1
- then calculate the current index_sum with indices[i] + i
.
If the current index_sum
is lower than previous than create a new result list, else if the sum is equal to current lowest, append the word to the result list.
Code
Python3
Big O Analysis
-
Runtime
The runtime complexity here is where N is the max of lengths of
list1
andlist2
. -
Memory
The memory usage is since use a hashtable to store words and their corresponding indices.
— A