P15363 [GeeksforGeeks] Closest strings0611
普及-
题目描述

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
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)
Constraints:
Sum of lengths of words ≤ 10^{5}
Note: word1 and word2 are both in the list.

输入格式

见题面

输出格式

见题面

样例输入
S = { "the", "quick", "brown", "fox", "quick"} word1 = "the" word2 = "fox"
样例输出
3