Tap here to switch tabs
Problem
Submissions

Problem: Shortest Word Distance III

med
30 min
Try to solve the Shortest Word Distance III problem.

Statement

Given an array of strings wordsDict and two strings word1 and word2, return the shortest distance between the occurrences of word1 and word2 in the array.

Note: word1 and word2 may be identical. When they are the same, they must refer to two distinct indices in the array.

Constraints:

  • 1≤1 \leq wordsDict.length ≤105\leq 10^5

  • 1≤1 \leq wordsDict[i].length ≤10\leq 10

  • wordsDict[i] consists of lowercase English letters.

  • word1 and word2 are in wordsDict.

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Shortest Word Distance III

med
30 min
Try to solve the Shortest Word Distance III problem.

Statement

Given an array of strings wordsDict and two strings word1 and word2, return the shortest distance between the occurrences of word1 and word2 in the array.

Note: word1 and word2 may be identical. When they are the same, they must refer to two distinct indices in the array.

Constraints:

  • 1≤1 \leq wordsDict.length ≤105\leq 10^5

  • 1≤1 \leq wordsDict[i].length ≤10\leq 10

  • wordsDict[i] consists of lowercase English letters.

  • word1 and word2 are in wordsDict.