Solution: Shortest Word Distance II
Let's solve the Shortest Word Distance II using the Custom Data Structures pattern.
We'll cover the following
Statement
Design a data structure that takes in an array of strings and efficiently computes the shortest distance between any two different strings in the array.
Implement the WordDistance
class:
WordDistance(String[] wordsDict)
: Initializes the object with an array of strings.int shortest(String word1, String word2)
: Returns the shortest distance betweenword1
andword2
in the array of strings.
Constraints:
wordsDict.length
wordsDict[i].length
wordsDict[i]
 consists of lowercase English lettersword1
 andÂword2
 are inÂwordsDict
word1
!=word2
At most,Â
 calls will be made to the shortest
Solution
The algorithm uses a custom data structure to efficiently calculate the shortest distance between words in an array. It starts by creating a dictionary that records the positions of each word in the input array, allowing for quick retrieval of word locations. When processing a query, the algorithm employs two pointers to compare the positions of the words. The pointer with the smaller index is always advanced, potentially bringing it closer to the other word and reducing the distance between the two positions. This approach ensures that all possible pairs of positions are considered, accurately calculating the shortest distance. The combination of preprocessing and smart traversal makes this algorithm an efficient solution for word distance queries.
Constructor
The
WordDistance
class uses a dictionary to store the indexes of each word in the givenwordsDict
array. This dictionary,wordIndices
, maps each word to a list of its positions (indexes) in the array.The Constructor method iterates over the
wordsDict
array, which provides both the index and the word at that index. For each word, it appends the index to the corresponding list in thewordIndices
dictionary.
The query for the shortest distance
The
shortest
method retrieves the lists of indexes for the two input words (word1
andword2
) from thewordIndices
dictionary.It initializes two pointers,
i
andj
, to traverse the lists of indexes (indices1
andindices2
). It also initializesminDistance
to infinity, holding the minimum distance found.The algorithm uses a two-pointer technique to find the minimum distance:
It compares the current elements pointed to by
i
andj
in the two lists (indices1[i]
andindices2[j]
).It updates
min_distance
to be the smaller of the currentminDistance
and the absolute difference betweenindices1[i]
andindices2[j]
.It moves the pointer that points to the smaller index value. This helps to gradually minimize the difference between the two indexes.
The loop continues until one of the lists is fully traversed. At this point,
minDistance
will hold the smallest distance between the positions of the two words.
Let’s look at the following illustration to get a better understanding of the solution:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.