0% completed
Solution: Shortest Word Distance
.....
.....
.....
Anonymous
· 2 years ago
and use two pointers to track the positions of these words.
this should be two variable and not "two pointer", it's confusion with 2 pointer pattern
Anonymous
· 2 years ago
Correct me if I'm wrong but consider this scenario.
["a", "c", "d", "b", "a", "f", "g", "h", "b"]
shortest distance would be returned as 4 instead of 1
Anonymous
· 3 years ago
String comparison is not a constant-time operation, so the loop that runs O(N) times is doing O(K) work every time. This matters because you could have a list of words in the order of tens or hundreds of words, but words in the order of thousands of characters since it wasn't specified that "words" must actually be English words and can't be full books, for instance.
Anonymous
· 3 years ago
Because to me it's not intuitive why by jumping with the pointers to any found instances of word1or word2 we guaranteed to handle all the cases and get the shortest distance
Anonymous
· 3 years ago
I'm not really seeing how this is a two pointer solution. It seems like you only loop through the array from start to finish to store the positions of the two words with a single pointer, and you update shortestDistance accordingly
Reading Progress
0%