class Solution {
public:
int shortestWordDistance(vector<string>& wordsDict, string word1, string word2) {
}
};
245. 最短单词距离 III
给定一个字符串数组 wordsDict
和两个字符串 word1
和 word2
,返回这两个单词在列表中出现的最短距离。
注意:word1
和 word2
是有可能相同的,并且它们将分别表示为列表中 两个独立的单词 。
示例 1:
输入:wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "coding" 输出:1
示例 2:
输入:wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "makes" 输出:3
提示:
1 <= wordsDict.length <= 105
1 <= wordsDict[i].length <= 10
wordsDict[i]
由小写英文字母组成word1
和 word2
都在 wordsDict
中原站题解