Similar Problems

Similar Problems not available

Shortest Word Distance - Leetcode Solution


LeetCode:  Shortest Word Distance Leetcode Solution

Difficulty: Easy

Topics: string array  

Problem Source: LeetCode
Companies: LinkedIn, Microsoft, Paypal, Oracle.

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


Assume that words = [“apple”, “oranges”, “mango”, “oranges”, “fruit”].

Input: word1 = “mango”, word2 = “apple”

Output: 2

Input: word1 = “oranges”, word2 = “fruit”

Output: 1

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

Shortest Word Distance Solution Code