X

DAYS

:

HOUR

:

MINS

:

SEC

Copied to Clipboard
Closest Strings
Easy Accuracy: 41.66% Submissions: 9889 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:

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


Example 2:

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


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)

Constraints:
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.