Problem Number: 245 Difficulty: Medium Category: Array, Two Pointers LeetCode Link: https://leetcode.com/problems/shortest-word-distance-iii/
Given an array of strings wordsDict and two strings word1 and word2, return the shortest distance between these two words in the list.
Note that word1 and word2 may be the same. It is guaranteed that they represent two individual words in the list.
Example 1:
Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "coding"
Output: 1
Example 2:
Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "makes"
Output: 3
Constraints:
1 <= wordsDict.length <= 3 * 10^41 <= wordsDict[i].length <= 10wordsDict[i]consists of lowercase English letters.word1andword2are inwordsDict.
I used a Hash Map with Two Pointers approach that handles the case where word1 and word2 can be the same. The key insight is to use different strategies for same and different words.
Algorithm:
- Create hash map to store word indices
- Collect indices for word1 and word2
- If word1 == word2:
- Use nested loops to find minimum distance between different indices
- If word1 != word2:
- Use two-pointer approach to find minimum distance
- Return minimum distance found
The solution uses hash map and handles same word case with nested loops. See the implementation in the solution file.
Key Points:
- Uses hash map to store word indices
- Handles same word case with nested loops
- Uses two-pointer approach for different words
- Avoids comparing same indices when words are identical
Time Complexity: O(n)
- Building hash map: O(n)
- Same word case: O(m²) where m is occurrences of the word
- Different words case: O(m + k) where m, k are occurrences
- Total: O(n)
Space Complexity: O(n)
- Hash map stores indices for all words
- Total: O(n)
-
Same Word Handling: When word1 == word2, need to find distance between different indices.
-
Nested Loops: For same word, use nested loops to compare different indices.
-
Two Pointers: For different words, use two-pointer approach.
-
Index Avoidance: When words are same, avoid comparing same index with itself.
-
Multiple Occurrences: Handles cases where words appear multiple times.
-
Efficient Lookup: Hash map provides O(1) lookup for word indices.
-
Same Word Case: Initially might not handle word1 == word2 case.
-
Wrong Comparison: Comparing same index with itself when words are identical.
-
Complex Logic: Overcomplicating the same word handling.
-
Inefficient Approach: Using same approach for both cases.
- Shortest Word Distance (Problem 243): Different words only
- Shortest Word Distance II (Problem 244): Design class version
- Two Sum (Problem 1): Find pair with target sum
- Container With Most Water (Problem 11): Two-pointer approach
- 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
- Same Word Case: Not handling word1 == word2 case properly.
- Wrong Comparison: Comparing same index with itself.
- Complex Logic: Overcomplicating the same word handling.
- Inefficient Approach: Using same approach for both cases.
- Memory Issues: Not considering space complexity of storing indices.
Note: This is a two-pointer problem that demonstrates handling same word case in distance calculation.