Last updated
Was this helpful?
Last updated
Was this helpful?
Given a list of words and two wordsword1_and_word2, return the shortest distance between these two words in the list.
Example:
Assume that words =["practice", "makes", "perfect", "coding", "makes"]
.
Note: You may assume thatword1does not equal toword2, and_word1_and_word2_are both in the list.
觉得两年前面L家的时候虽然是把S:O(1),T:O(N)过一遍的解法背下来了但没吃透这一题,。先贴那个。
如今二刷发现这题水很深。最intuitive的是把位置存起来。然后这题就变成了two pointer求两个sorted array里的min diff了。T:O(N) 因为过了两遍,S:O(N)因为用了hashmap存了位置。