Given an array of strings wordsDict
and two different strings that already exist in the array word1
and word2
, return the shortest distance between these two words in the list.
Example 1:
Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "coding", word2 = "practice" Output: 3
Example 2:
Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "coding" Output: 1
Constraints:
2 <= wordsDict.length <= 3 * 104
1 <= wordsDict[i].length <= 10
wordsDict[i]
consists of lowercase English letters.word1
and word2
are different.word1
and word2
are both in the list.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 goal is to find the smallest distance between two specific words in a list of words. The brute force method simply checks the distance between every possible pair of those two words in the list.
Here's how the algorithm would work step-by-step:
def shortest_word_distance_brute_force(words_list, word1, word2):
minimum_distance = float('inf')
for first_word_index in range(len(words_list)):
if words_list[first_word_index] == word1:
# Found an instance of word1, now find word2
for second_word_index in range(len(words_list)):
if words_list[second_word_index] == word2:
# Calculate distance and update minimum
current_distance = abs(first_word_index - second_word_index)
minimum_distance = min(minimum_distance, current_distance)
return minimum_distance
The most efficient way to find the shortest distance between two specific words in a list involves walking through the list only once. We keep track of the most recent positions where each of the two target words appears, updating the shortest distance whenever we find both words.
Here's how the algorithm would work step-by-step:
def shortest_word_distance(words_list, word1, word2):
last_position_word1 = -1
last_position_word2 = -1
shortest_distance = float('inf')
for i, word in enumerate(words_list):
if word == word1:
last_position_word1 = i
if word == word2:
last_position_word2 = i
# Need to check if both words have been found
if last_position_word1 != -1 and last_position_word2 != -1:
# Ensures correct calculation with absolute difference
current_distance = abs(last_position_word1 - last_position_word2)
# Update shortest distance if needed
shortest_distance = min(shortest_distance, current_distance)
return shortest_distance
Case | How to Handle |
---|---|
wordsDict is null or empty | Return a large value like Integer.MAX_VALUE or throw an exception to indicate no distance is possible. |
word1 or word2 is null or empty | Throw an IllegalArgumentException as it is not defined behavior |
wordsDict contains only word1 | Search for word2 and if it is not present, return Integer.MAX_VALUE |
wordsDict contains only word2 | Search for word1 and if it is not present, return Integer.MAX_VALUE |
word1 and word2 are the same string | Find the two closest occurrences of the word and return the distance. |
word1 or word2 is not present in wordsDict | Return Integer.MAX_VALUE since one of the words cannot be found. |
Integer overflow with large index differences | Use long data type to store the distance to prevent potential integer overflow. |
Large wordsDict size | Iterative solution will be more efficient than recursive calls and will handle a large input better. |