|
| 1 | +--- |
| 2 | +id: shortest-word-distance |
| 3 | +title: Shortest Word Distance Solution |
| 4 | +sidebar_label: 0243 Shortest Word Distance |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Two Pointers |
| 8 | + - LeetCode |
| 9 | + - Python |
| 10 | + - Java |
| 11 | + - C++ |
| 12 | +description: "This is a solution to the Shortest Word Distance problem on LeetCode." |
| 13 | +--- |
| 14 | + |
| 15 | +## Problem Description |
| 16 | + |
| 17 | +Given a list of words `words` and two words `word1` and `word2`, return the shortest distance between these two words in the list. |
| 18 | + |
| 19 | +### Examples |
| 20 | + |
| 21 | +**Example 1:** |
| 22 | +``` |
| 23 | +Input: words = ["practice", "makes", "perfect", "coding", "makes"], word1 = "coding", word2 = "practice" |
| 24 | +Output: 3 |
| 25 | +``` |
| 26 | + |
| 27 | +### Constraints |
| 28 | + |
| 29 | +- You may assume that `words` contains at least two words. |
| 30 | +- All the words in `words` are guaranteed to be unique. |
| 31 | +- `word1` and `word2` are two distinct words in the list. |
| 32 | + |
| 33 | +## Solution for Shortest Word Distance Problem |
| 34 | + |
| 35 | +### Approach |
| 36 | + |
| 37 | +To find the shortest distance between two words `word1` and `word2` in a list of words `words`, we can use the following approach: |
| 38 | + |
| 39 | +1. **Two Pointers**: Use two pointers to track the most recent indices of `word1` and `word2` as we iterate through the list. |
| 40 | +2. **Update Distance**: For each occurrence of `word1` or `word2`, update the minimum distance found so far if possible. |
| 41 | + |
| 42 | +### Code in Different Languages |
| 43 | + |
| 44 | +<Tabs> |
| 45 | + <TabItem value="Python" label="Python" default> |
| 46 | + <SolutionAuthor name="@mahek0620"/> |
| 47 | + ```python |
| 48 | + |
| 49 | + class Solution: |
| 50 | + def shortestDistance(self, words: List[str], word1: str, word2: str) -> int: |
| 51 | + index1 = -1 |
| 52 | + index2 = -1 |
| 53 | + min_distance = float('inf') |
| 54 | + |
| 55 | + for i in range(len(words)): |
| 56 | + if words[i] == word1: |
| 57 | + index1 = i |
| 58 | + elif words[i] == word2: |
| 59 | + index2 = i |
| 60 | + |
| 61 | + if index1 != -1 and index2 != -1: |
| 62 | + min_distance = min(min_distance, abs(index1 - index2)) |
| 63 | + |
| 64 | + return min_distance |
| 65 | + ``` |
| 66 | + </TabItem> |
| 67 | + <TabItem value="Java" label="Java"> |
| 68 | + <SolutionAuthor name="@mahek0620"/> |
| 69 | + ```java |
| 70 | + |
| 71 | + import java.util.*; |
| 72 | + |
| 73 | + class Solution { |
| 74 | + public int shortestDistance(String[] words, String word1, String word2) { |
| 75 | + int index1 = -1; |
| 76 | + int index2 = -1; |
| 77 | + int minDistance = Integer.MAX_VALUE; |
| 78 | + |
| 79 | + for (int i = 0; i < words.length; i++) { |
| 80 | + if (words[i].equals(word1)) { |
| 81 | + index1 = i; |
| 82 | + } else if (words[i].equals(word2)) { |
| 83 | + index2 = i; |
| 84 | + } |
| 85 | + |
| 86 | + if (index1 != -1 && index2 != -1) { |
| 87 | + minDistance = Math.min(minDistance, Math.abs(index1 - index2)); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return minDistance; |
| 92 | + } |
| 93 | + } |
| 94 | + ``` |
| 95 | + </TabItem> |
| 96 | + <TabItem value="C++" label="C++"> |
| 97 | + <SolutionAuthor name="@mahek0620"/> |
| 98 | + ```cpp |
| 99 | + |
| 100 | + #include <vector> |
| 101 | + #include <string> |
| 102 | + #include <algorithm> |
| 103 | + #include <climits> |
| 104 | + using namespace std; |
| 105 | + |
| 106 | + class Solution { |
| 107 | + public: |
| 108 | + int shortestDistance(vector<string>& words, string word1, string word2) { |
| 109 | + int index1 = -1; |
| 110 | + int index2 = -1; |
| 111 | + int minDistance = INT_MAX; |
| 112 | + |
| 113 | + for (int i = 0; i < words.size(); i++) { |
| 114 | + if (words[i] == word1) { |
| 115 | + index1 = i; |
| 116 | + } else if (words[i] == word2) { |
| 117 | + index2 = i; |
| 118 | + } |
| 119 | + |
| 120 | + if (index1 != -1 && index2 != -1) { |
| 121 | + minDistance = min(minDistance, abs(index1 - index2)); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return minDistance; |
| 126 | + } |
| 127 | + }; |
| 128 | + ``` |
| 129 | + |
| 130 | + </TabItem> |
| 131 | +</Tabs> |
| 132 | + |
| 133 | +### Complexity Analysis |
| 134 | + |
| 135 | +- **Time complexity**: O(n), where n is the number of elements in the `words` array. We iterate through the array once. |
| 136 | +- **Space complexity**: O(1) in additional space, as we use only a few extra variables regardless of the input size. |
| 137 | + |
| 138 | +## References |
| 139 | + |
| 140 | +- **LeetCode Problem:** [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/) |
| 141 | +- **Solution Link:** [Shortest Word Distance Solution on LeetCode](https://leetcode.com/problems/shortest-word-distance/solution/) |
| 142 | +- **Author's GeeksforGeeks Profile:** [Mahek Patel](https://leetcode.com/u/mahekrpatel611/) |
| 143 | + |
| 144 | + |
0 commit comments