You have a large text file containing words. Given any two words, find the shortest distance (in terms of number of words) between them in the file. If the operation will be repeated many times for the same file (but different pairs of words), can you optimize your solution?
Closest distance between two words. Pairing every index of each word can be quadratic.
The closest pair always uses some occurrence and the latest occurrence of the other word, so one scan suffices.
\(i\) and \(j\) store the latest indices; each step updates \(|i-j|\). Only the most recent sighting of each word is kept.
We use two pointers \(i\) and \(j\) to record the most recent occurrences of the two words \(\textit{word1}\) and \(\textit{word2}\), respectively. Initially, \(i = \infty\) and \(j = -\infty\).
Next, we traverse the entire text file. For each word \(w\), if \(w\) equals \(\textit{word1}\), we update \(i = k\), where \(k\) is the index of the current word; if \(w\) equals \(\textit{word2}\), we update \(j = k\). Then we update the answer \(ans = \min(ans, |i - j|)\).
After the traversal, we return the answer \(ans\).
The time complexity is \(O(n)\), where \(n\) is the number of words in the text file. The space complexity is \(O(1)\).
A single pass fits one query; many queries on the same text would rescan everything.
Precompute index lists and, per query, two-pointer the two sorted lists. Time follows the number of hits.
We can use a hash table \(d\) to record the positions of each word. Then, for each pair of \(\textit{word1}\) and \(\textit{word2}\), we can find their shortest distance using the two-pointer method.
We traverse the entire text file. For each word \(w\), we add the index of \(w\) to \(d[w]\).
Next, we find the positions where \(\textit{word1}\) and \(\textit{word2}\) appear in the text file, represented by \(idx1\) and \(idx2\) respectively. Then we use two pointers \(i\) and \(j\) to point to \(idx1\) and \(idx2\) respectively, with initial values \(i = 0\), \(j = 0\).
Next, we traverse \(idx1\) and \(idx2\). Each time we update the answer \(ans = \min(ans, |idx1[i] - idx2[j]|)\), then we move the smaller pointer of \(i\) and \(j\) one step backward.
The time complexity is \(O(n)\), and the space complexity is \(O(n)\). Where \(n\) is the number of words in the text file.