Shortest Word Distance - Problem

Imagine you're reading through a document and need to find how close two specific words appear to each other. Given an array of strings wordsDict representing words in a document and two different strings word1 and word2 that exist in the array, your task is to find the shortest distance between these two words.

The distance is defined as the absolute difference between the indices where the words appear. For example, if word1 appears at index 1 and word2 appears at index 4, the distance is |4 - 1| = 3.

Goal: Return the minimum possible distance between any occurrence of word1 and word2 in the given array.

Input & Output

example_1.py โ€” Basic Case
$ Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"] word1 = "coding", word2 = "practice"
โ€บ Output: 3
๐Ÿ’ก Note: "coding" appears at index 3 and "practice" appears at index 0, so the distance is |3 - 0| = 3.
example_2.py โ€” Multiple Occurrences
$ Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"] word1 = "makes", word2 = "coding"
โ€บ Output: 1
๐Ÿ’ก Note: "makes" appears at indices 1 and 4, "coding" appears at index 3. The shortest distance is between indices 4 and 3: |4 - 3| = 1.
example_3.py โ€” Adjacent Words
$ Input: wordsDict = ["a", "b", "c"] word1 = "a", word2 = "b"
โ€บ Output: 1
๐Ÿ’ก Note: "a" is at index 0 and "b" is at index 1, so the distance is |1 - 0| = 1. This is the minimum possible distance for different words.

Visualization

Tap to expand
Optimal One-Pass Solution["practice", "makes", "perfect", "coding", "makes"] โ†’ find distance("makes", "perfect")practicemakes โœ“perfect โœ“codingmakes โœ“i=0i=1i=2i=3i=4Algorithm Steps:1. i=0: "practice" โ†’ skip2. i=1: "makes" โ†’ pos1=13. i=2: "perfect" โ†’ pos2=2 โ†’ min_dist = |1-2| = 14. i=3: "coding" โ†’ skip5. i=4: "makes" โ†’ pos1=4 โ†’ min_dist = min(1, |4-2|) = 1distance = 1RESULT1โšก O(n) time, O(1) space - Optimal!
Understanding the Visualization
1
Start walking
Begin traversing the array, looking for target words
2
Mark positions
When we find word1 or word2, remember their positions
3
Calculate distance
If both words have been seen, calculate current distance
4
Update minimum
Keep the smallest distance found so far
Key Takeaway
๐ŸŽฏ Key Insight: We only need to track the most recent positions of both target words. Older positions will never yield a shorter distance than the current closest pair, allowing us to solve this efficiently in a single pass.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Single pass through the array, checking each element once

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for position tracking variables

n
2n
โœ“ Linear Space

Constraints

  • 2 โ‰ค wordsDict.length โ‰ค 3 ร— 104
  • 1 โ‰ค wordsDict[i].length โ‰ค 10
  • wordsDict[i] consists of lowercase English letters
  • word1 and word2 are in wordsDict
  • word1 โ‰  word2
Asked in
Google 12 Amazon 8 LinkedIn 6 Microsoft 4
42.2K Views
Medium Frequency
~12 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen