← 返回 uber 的题目列表Shortest Word Distance
类型:qbank
Given an array of strings and two distinct words that both appear in it, return the minimum index distance between any occurrence of the two words.
Shortest Word Distance
Given an array of strings and two distinct words that both appear in it, return the minimum index distance between any occurrence of the two words.
SWE
array
two-pointer
string
easy
Frequency
Single report
Last asked
2026-03-13
Stage
phone-screen
Shortest Word Distance
You are given an array of strings wordsDict and two different strings word1 and word2 that both appear in the array.
Return the minimum distance between any occurrence of word1 and any occurrence of word2 in wordsDict.
Examples
Example 1:
Input: wordsDict = ["practice","makes","perfect","coding","makes"], word1 = "coding", word2 = "practice"
Output: 3
Example 2:
Input: wordsDict = ["practice","makes","perfect","coding","makes"], word1 = "makes", word2 = "coding"
Output: 1
Constraints
1 <= wordsDict.length <= 3 * 10^4
1 <= wordsDict[i].length <= 10
word1 != word2
word1 and word2 both appear in wordsDict.