Back to course home
0% completed
Vote For New Content
Shortest Word Distance (easy)
Problem Statement
Given an array of strings words
and two different strings that already exist in the array word1
and word2
, return the shortest distance between these two words in the list.
Example 1:
Input: words = ["the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"], word1 = "fox", word2 = "dog"
Output: 5
Explanation: The distance between "fox" and "dog" is 5 words.
Example 2:
Input: words = ["a", "c", "d", "b", "a"], word1 = "a", word2 = "b"
Output: 1
Explanation: The shortest distance between "a" and "b" is 1 word. Please note that "a" appeared twice.
Example 3:
Input: words = ["a", "b", "c", "d", "e"], word1 = "a", word2 = "e"
Output: 4
Explanation: The distance between "a" and "e" is 4 words.
Constraints:
- 2 <= words.length <= 3 * 10<sup>4</sup>
1 <= words[i].length <= 10
words[i]
consists of lowercase English letters.word1
andword2
are in words.word1 != word2
Try it yourself
Try solving this question here:
Python3
Python3
. . . .
.....
.....
.....
Like the course? Get enrolled and start learning!
Table of Contents
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible
Contents are not accessible