Solved Leetcode 245. Shortest Word Distance III

source: https://leetcode.com/problems/shortest-word-distance-iii/description/

Shortest Word Distance III

Given an array of strings wordsDict and two strings that already exist in the array word1 and word2, return the shortest distance between the occurrence of these two words in the list.

Note that word1 and word2 may be the same. It is guaranteed that they represent two individual words in the list.

Example 1:
Input: wordsDict = [“practice”, “makes”, “perfect”, “coding”, “makes”], word1 = “makes”, word2 = “coding”
Output: 1

Example 2:
Input: wordsDict = [“practice”, “makes”, “perfect”, “coding”, “makes”], word1 = “makes”, word2 = “makes”
Output: 3

Constraints:

  • 1 <= wordsDict.length <= 105
  • 1 <= wordsDict[i].length <= 10
  • wordsDict[i] consists of lowercase English letters.
    word1 and word2 are in wordsDict.

Let’s say, wordsDict contains n elements

Time Complexity
O(n)

Space Complexity
O(2n) -> O(n)

Rate this post

Leave a Reply