# Shortest Word Distance

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

For example, Assume that

> words = \["practice", "makes", "perfect", "coding", "makes"].

Given

> word1 = “coding”, \
> word2 = “practice”, \
> return 3.

Given

> word1 = "makes", \
> word2 = "coding", \
> return 1.

Note: You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.

#### [Shortest Word Distance II](https://leetcode.com/problems/shortest-word-distance-ii/)

The only difference is now you are given the list of words and your method will be called repeatedly many times with different parameters. How would you optimize it?

Design a class which receives a list of words in the constructor, and implements a method that takes two words word1 and word2 and return the shortest distance between these two words in the list.

#### [Shortest Word Distance III](https://leetcode.com/problems/shortest-word-distance-iii/)

The only difference is now word1 could be the same as word2.

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

word1 and word2 may be the same and they represent two individual words in the list.

For example, Assume that

> words = \["practice", "makes", "perfect", "coding", "makes"].

Given

> word1 = “makes”, word2 = “coding”, return 1.

Given

> word1 = "makes", word2 = "makes", return 3.

{% tabs %}
{% tab title="Scala" %}

```scala
def shortestDistance(words: Array[String], word1: String, word2: String) = {
  var minDist = Int.MaxValue
  var (word1Index, word2Index) = (-1, -1)
  for ((word, i) <- words.zipWithIndex) {
    if (word == word1) word1Index = i
    else if (word == word2) word2Index = i
    if (word1Index != -1 && word2Index != -1) minDist = minDist min math.abs(word1Index - word2Index)
  }
  minDist
}

val words = Array("practice", "makes", "perfect", "coding", "makes")
shortestDistance(words, "coding", "practice")
shortestDistance(words, "makes", "coding")

/**
  * Follow up
  * https://leetcode.com/problems/shortest-word-distance-ii/
  *
  * @param words
  */
class WordDistance(words: Array[String]) {

  val indexes: Map[String, Array[Int]] = (for {
    (word, i) <- words zipWithIndex
  } yield (word, i)).groupBy(_._1).mapValues(_.map(_._2))

  def shortest(word1: String, word2: String): Int = {
    var minDist = Int.MaxValue
    for (x <- indexes(word1); y <- indexes(word2)) {
      minDist = minDist min math.abs(x - y)
    }
    minDist
  }

}

val wd = new WordDistance(words)
wd.shortest("coding", "practice")
wd.shortest("makes", "coding")

/**
  * Follow up
  *
  * @param words
  * @param word1
  * @param word2
  * @return
  */
def shortestDistanceIII(words: Array[String], word1: String, word2: String) = {
  var minDist = Int.MaxValue
  var (word1Index, word2Index) = (-1, -1)
  val sameWord: Boolean = word1 == word2
  for ((word, i) <- words.zipWithIndex) {
    if (sameWord && word == word1) {
      word1Index = word2Index
      word2Index = i
    } else {
      if (word == word1) word1Index = i
      else if (word == word2) word2Index = i
    }
    if (word1Index != -1 && word2Index != -1) minDist = minDist min math.abs(word1Index - word2Index)
  }
  minDist
}


shortestDistanceIII(words, "makes", "coding")
shortestDistanceIII(words, "makes", "makes")
```

{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://blog.bernardw.com/problems/shortest-word-distance.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
