Shortest Word Distance II - Problem

You're building a smart dictionary system that needs to quickly find the shortest distance between any two words in a text corpus. Given an array of strings representing words, you need to design a data structure that can efficiently answer multiple queries about the minimum distance between different word pairs.

The Challenge: Design the WordDistance class that supports:

  • WordDistance(String[] wordsDict) - Initialize with an array of words
  • int shortest(String word1, String word2) - Return the shortest distance between word1 and word2

The distance between two words is the absolute difference between their indices in the array. Since multiple queries will be made, your solution should optimize for query time efficiency.

Example: If wordsDict = ["practice", "makes", "perfect", "coding", "makes"], then shortest("makes", "coding") should return 1 because the closest occurrence of "makes" is at index 4 and "coding" is at index 3, giving us |4-3| = 1.

Input & Output

example_1.py โ€” Basic Usage
$ Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"] word1 = "makes", word2 = "coding"
โ€บ Output: 1
๐Ÿ’ก Note: The word "makes" appears at indices 1 and 4, while "coding" appears at index 3. The shortest distance is between indices 4 and 3, which is |4-3| = 1.
example_2.py โ€” Multiple Queries
$ Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"] word1 = "practice", word2 = "makes"
โ€บ Output: 1
๐Ÿ’ก Note: "practice" is at index 0, "makes" appears at indices 1 and 4. The shortest distance is between indices 0 and 1, which is |0-1| = 1.
example_3.py โ€” Distant Words
$ Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"] word1 = "practice", word2 = "perfect"
โ€บ Output: 2
๐Ÿ’ก Note: "practice" is at index 0, "perfect" is at index 2. The distance is |0-2| = 2.

Visualization

Tap to expand
๐Ÿ“š Smart Library Catalog SystemLibrary Shelves (Word Array)Book ABook BBook CBook DBook BBook EBook A๐Ÿ“‹ Catalog (Hash Map)Book A: [shelf 0, shelf 6]Book B: [shelf 1, shelf 4]Book C: [shelf 2]Book D: [shelf 3]Book E: [shelf 5]Built once during initialization๐Ÿค Two-Pointer QueryQuery: Distance between Book B and Book D?Book B positions: [1, 4]Book D positions: [3]1ptr143ptr2Distance: min(|1-3|, |4-3|) = 1โšก Fast queries after one-time preprocessing!
Understanding the Visualization
1
Build Catalog
Create a comprehensive index mapping each book title to all shelf positions
2
Quick Lookup
When asked about two books, instantly retrieve their position lists from the catalog
3
Smart Comparison
Use two-finger technique to efficiently find the minimum walking distance
Key Takeaway
๐ŸŽฏ Key Insight: Preprocess once with hash map, then use two pointers for O(m+n) queries - perfect for systems with many lookups!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n) preprocessing, O(m + n) per query

Preprocessing takes O(n) to build hash map. Each query takes O(m + n) where m and n are frequencies of the two words

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

Hash map stores all positions, which in worst case is O(n) space

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค wordsDict.length โ‰ค 3 ร— 104
  • 1 โ‰ค wordsDict[i].length โ‰ค 10
  • wordsDict[i] consists of lowercase English letters
  • word1 and word2 are in wordsDict
  • word1 โ‰  word2
  • At most 5000 calls will be made to shortest
Asked in
Google 42 Amazon 38 Meta 25 Microsoft 18
42.3K Views
Medium Frequency
~15 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