0245. Shortest Word Distance III
https://leetcode.com/problems/shortest-word-distance-iii
Description
Given an array of strings wordsDict and two 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:
**Input:** wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "coding"
**Output:** 1Example 2:
**Input:** wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "makes"
**Output:** 3Constraints:
1 <= wordsDict.length <= 1051 <= wordsDict[i].length <= 10wordsDict[i]consists of lowercase English letters.word1andword2are inwordsDict.
ac2
this one is stupid, why you need to have a map? a waste time and space.
ac2
ac3
Last updated
Was this helpful?