Skip to main content

Shortest Distance Between Words

Problem Statement​

Problem Description​

Design a data structure that can compute the shortest distance between any two distinct strings within an array of strings. Initialize the data structure with an array of strings, wordsDict. Once initialized, it should return the smallest possible index difference between two different strings in wordsDict when queried.

Examples​

Example 1:

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

Example 2:

Input: wordsDict = ["practice", "makes", "perfect", "coding", "makes"]
Query: word1 = "makes", word2 = "coding"
Output: 1

Constraints​

  • The number of words in wordsDict is in the range [1, 10^5].
  • wordsDict[i] consists of lowercase English letters.
  • word1 and word2 are distinct and will always be in wordsDict.

Solution of Given Problem​

Intuition and Approach​

To efficiently find the shortest distance between two words in the dictionary, a preprocessing step is required during initialization. We traverse the wordsDict array once and create a hash map where keys are the distinct words from the array, and values are lists of indices where each key word is located in the original array. This preprocessing step allows for a quick lookup of the positions of any word, facilitating the computation of the distances between any two words.

Once the positions are mapped, to find the shortest distance between word1 and word2, we get their list of indices from our hash map. We need to find the minimum difference between any two indices from these lists. The lists are already sorted because the indices were appended in the order they were encountered during initialization.

A two-pointer approach efficiently solves the problem of finding the minimum difference. Start with the first index of each list, and at each step, move the pointer that points to the smaller index to the next index in its list. This approach will traverse the two lists simultaneously and will always give the smallest possible difference in indices (thus the shortest distance) between word1 and word2. The process repeats until we have fully traversed one of the lists, ensuring that no potential shorter distance is missed.

Approaches​

Codes in Different Languages​

Written by @pallasivasai
 class WordDistance {
constructor(wordsDict) {
this.word_positions = {};
for (let index = 0; index < wordsDict.length; index++) {
const word = wordsDict[index];
if (!this.word_positions[word]) {
this.word_positions[word] = [];
}
this.word_positions[word].push(index);
}
}

shortest(word1, word2) {
const positions1 = this.word_positions[word1];
const positions2 = this.word_positions[word2];
let i = 0, j = 0, min_distance = Infinity;

while (i < positions1.length && j < positions2.length) {
const index1 = positions1[i], index2 = positions2[j];
min_distance = Math.min(min_distance, Math.abs(index1 - index2));
if (index1 < index2) {
i++;
} else {
j++;
}
}

return min_distance;
}
}

// Example Usage
const wordsDict = ["practice", "makes", "perfect", "coding", "makes"];
const wordDistance = new WordDistance(wordsDict);
console.log(wordDistance.shortest("coding", "practice")); // Output: 3
console.log(wordDistance.shortest("makes", "coding")); // Output: 1

Explanation​

  1. Initialization (Constructor):

    • We iterate through the wordsDict array and populate the wordPositions map with arrays of indices for each word. This allows for quick lookups of the positions of any word in the list.
  2. Shortest Distance (shortest method):

    • Retrieve the arrays of indices for word1 and word2 from the map.
    • Use two pointers (i and j) to traverse the arrays and compute the minimum distance. This two-pointer technique efficiently finds the minimum index difference by advancing the pointer that points to the smaller index.

Complexity​

  • Initialization: O(n), where n is the number of words in wordsDict.
  • Querying: O(m + k), where m and k are the lengths of the arrays of indices for word1 and word2 respectively. This ensures that the queries are processed efficiently after the initial setup.

References​

Authors:

Loading...
Loading...