Closest Strings
Easy Accuracy: 27.93% Submissions: 5245 Points: 2

Given a list of words followed by two words, the task to find the minimum distance between the given two words in the list of words

Example 1:

S = { "the", "quick", "brown", "fox", 
word1 = "the"
word2 = "fox"
Output: 3
Explanation: Minimum distance between the 
words "the" and "fox" is 3

Example 2:

S = {"geeks", "for", "geeks", "contribute", 
word1 = "geeks"
word2 = "practice"
Output: 2
Explanation: Minimum distance between the
words "geeks" and "practice" is 2

Your Task:  
You don't need to read input or print anything. Your task is to complete the function

shortestDistance() which list of words, two strings as inputs and returns the minimum distance between two words

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(1)

Sum of lengths of words ≤ 105

Note: word1 and word2 are both in the list.

We are replacing the old Disqus forum with the new Discussions section given below.
Click here to view old Disqus comments.

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.

Closest Strings

Output Window