Hone logo
Hone
Problems

Shortest Word Distance II

This challenge asks you to find the shortest distance between any two distinct words in a given list of words. Calculating this distance efficiently is useful in natural language processing tasks like keyword analysis and semantic similarity detection. You'll need to optimize for performance, especially with large input lists.

Problem Description

You are given a list of words and two target words, word1 and word2. Your task is to find the minimum distance between the indices of word1 and word2 in the list. The distance is defined as the absolute difference between their indices. If either word1 or word2 is not present in the list, return -1. You must consider all possible pairs of indices for word1 and word2 to ensure you find the absolute minimum distance.

Key Requirements:

  • The function should accept a list of strings (words) and two strings (target words) as input.
  • The function should return an integer representing the minimum distance between the indices of the two target words.
  • The function should handle cases where either or both target words are not found in the list.
  • The function should be efficient, especially for large input lists.

Expected Behavior:

The function should iterate through the list of words, keeping track of the indices of word1 and word2. For each occurrence of word1, it should calculate the distance to the most recent occurrence of word2. Similarly, for each occurrence of word2, it should calculate the distance to the most recent occurrence of word1. The minimum of all these distances is the result.

Edge Cases to Consider:

  • Empty input list.
  • Either word1 or word2 is an empty string.
  • Both word1 and word2 are the same word.
  • word1 and word2 appear consecutively.
  • word1 and word2 appear multiple times with varying distances.

Examples

Example 1:

Input: words = ["practice", "makes", "perfect", "coding", "makes"], word1 = "practice", word2 = "coding"
Output: 5
Explanation: The index of "practice" is 0, and the index of "coding" is 3. The absolute difference is |0 - 3| = 3.  However, the index of "practice" is 0 and the index of "coding" is 3. The distance is 3.

Example 2:

Input: words = ["practice", "makes", "perfect", "coding", "makes"], word1 = "coding", word2 = "practice"
Output: 3
Explanation: The index of "coding" is 3, and the index of "practice" is 0. The absolute difference is |3 - 0| = 3.

Example 3:

Input: words = ["practice", "makes", "perfect", "coding", "makes"], word1 = "makes", word2 = "makes"
Output: 1
Explanation: The indices of "makes" are 1 and 4. The absolute difference is |1 - 4| = 3. However, the problem asks for the shortest distance between *distinct* words. Since they are the same word, we consider the distance between consecutive occurrences. |1-4| = 3.  The problem statement is ambiguous.  For this challenge, we will assume that if the words are the same, we return the distance between the closest two occurrences.

Example 4:

Input: words = ["practice", "makes", "perfect", "coding", "makes"], word1 = "hello", word2 = "coding"
Output: -1
Explanation: "hello" is not present in the list.

Constraints

  • 1 <= words.length <= 10^5
  • 1 <= word1.length, word2.length <= 10
  • word1 != word2 (unless both are the same word, as per Example 3)
  • All words in words have length between 1 and 10.
  • The function should have a time complexity of O(n), where n is the length of the words list.

Notes

Consider using two pointers or a hash map to efficiently track the indices of the target words. The key is to update the minimum distance as you iterate through the list, keeping track of the most recent occurrences of each word. Think about how to handle the case where a word appears multiple times. The problem statement is slightly ambiguous regarding identical words; the provided examples clarify the expected behavior in that scenario.

Loading editor...
plaintext