245. Shortest Word Distance III
LeetCode 245. Shortest Word Distance III
Description
Given an array of strings wordsDict
and two different strings that already exist in the array word1
and word2
, return the shortest distance between these two words in the list.
Note that word1
and word2
may be the same. It is guaranteed that they represent two individual words in the list.
Example 1:
Example 2:
Constraints:
1 <= wordsDict.length <= 3 * 104
1 <= wordsDict[i].length <= 10
wordsDict[i]
consists of lowercase English letters.word1
andword2
are inwordsDict
.
Tags
Array
Solution
Based on the answer of LeetCode 243. Shortest Word Distance, we add a statement for the case when word1
and word2
are identical.
Complexity
Time complexity:
Space complexity:
Code
Last updated
Was this helpful?