Skip to main content

Shortest Word Distance Solution

Problem Description​

Given a list of words words and two words word1 and word2, return the shortest distance between these two words in the list.

Examples​

Example 1:

Input: words = ["practice", "makes", "perfect", "coding", "makes"], word1 = "coding", word2 = "practice"
Output: 3

Constraints​

  • You may assume that words contains at least two words.
  • All the words in words are guaranteed to be unique.
  • word1 and word2 are two distinct words in the list.

Solution for Shortest Word Distance Problem​

Approach​

To find the shortest distance between two words word1 and word2 in a list of words words, we can use the following approach:

  1. Two Pointers: Use two pointers to track the most recent indices of word1 and word2 as we iterate through the list.
  2. Update Distance: For each occurrence of word1 or word2, update the minimum distance found so far if possible.

Code in Different Languages​

Written by @mahek0620

class Solution:
def shortestDistance(self, words: List[str], word1: str, word2: str) -> int:
index1 = -1
index2 = -1
min_distance = float('inf')

for i in range(len(words)):
if words[i] == word1:
index1 = i
elif words[i] == word2:
index2 = i

if index1 != -1 and index2 != -1:
min_distance = min(min_distance, abs(index1 - index2))

return min_distance

Complexity Analysis​

  • Time complexity: O(n), where n is the number of elements in the words array. We iterate through the array once.
  • Space complexity: O(1) in additional space, as we use only a few extra variables regardless of the input size.

References​