4.4 KiB
Shortest Word Distance
Problem Number: 243 Difficulty: Easy Category: Array, Two Pointers LeetCode Link: https://leetcode.com/problems/shortest-word-distance/
Problem Description
Given an array of strings wordsDict and two different strings word1 and word2, return the shortest distance between these two words in the list.
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^41 <= wordsDict[i].length <= 10word1andword2are inwordsDict.word1 != word2
My Approach
I used a Hash Map with Two Pointers approach to find the shortest distance. The key insight is to collect all indices of both words and then use two pointers to find the minimum distance.
Algorithm:
- Create hash map to store word indices
- Collect all indices for word1 and word2
- Use two pointers to traverse both index lists
- Calculate distance between current indices
- Move pointer with smaller index to find minimum distance
- Return minimum distance found
Solution
The solution uses hash map and two pointers to efficiently find shortest distance. See the implementation in the solution file.
Key Points:
- Uses hash map to store word indices
- Two-pointer approach for efficient traversal
- Calculates minimum distance between all pairs
- Handles multiple occurrences of words
Time & Space Complexity
Time Complexity: O(n)
- Building hash map: O(n)
- Two-pointer traversal: O(m + k) where m, k are occurrences of word1, word2
- Total: O(n)
Space Complexity: O(n)
- Hash map stores indices for all words
- In worst case: O(n)
Key Insights
-
Index Collection: Collecting all indices allows efficient distance calculation.
-
Two Pointers: Using two pointers on sorted indices is optimal.
-
Sorted Indices: Indices are naturally sorted in order of appearance.
-
Minimum Distance: Always move the pointer with smaller index.
-
Multiple Occurrences: Handles cases where words appear multiple times.
-
Efficient Traversal: Two-pointer approach avoids checking all pairs.
Mistakes Made
-
Brute Force: Initially might check all pairs of indices.
-
Wrong Order: Not understanding the two-pointer traversal order.
-
Complex Logic: Overcomplicating the distance calculation.
-
Inefficient Approach: Using nested loops instead of two pointers.
Related Problems
- Shortest Word Distance II (Problem 244): Design class for multiple queries
- Shortest Word Distance III (Problem 245): Handle case where word1 == word2
- Two Sum (Problem 1): Find pair with target sum
- Container With Most Water (Problem 11): Two-pointer approach
Alternative Approaches
- Single Pass: Track last seen indices during single pass - O(n) time, O(1) space
- Binary Search: Use binary search on sorted indices - O(n log n) time
- Brute Force: Check all pairs of indices - O(n²) time
Common Pitfalls
- Brute Force: Using nested loops to check all pairs.
- Wrong Order: Not understanding the two-pointer traversal order.
- Complex Logic: Overcomplicating the distance calculation.
- Inefficient Approach: Using O(n²) approach when O(n) is possible.
- Memory Issues: Not considering space complexity of storing indices.
Note: This is a two-pointer problem that demonstrates efficient distance calculation between word indices.