← 返回 amazon 的题目列表Shortest Word Distance
类型:online_judge
Shortest Word Distance
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
Input:
A list words of strings.
Two strings word1 and word2.
Output:
An integer representing the shortest distance between word1 and word2 in the list.
Note:
Both word1 and word2 are in the list words.
Constraints:
The length of words is between [2, 10^4].
Example:
Input: words = ["practice", "makes", "perfect", "coding", "makes"], word1 = "coding", word2 = "practice"
Output: 3
Input: words = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "coding"
Output: 1
Example
Input
practice makes perfect coding makes
coding
practice