Find the minimum distance between the given two words
Last Updated :
12 Jul, 2023
Given a list of words followed by two words, the task is to find the minimum distance between the given two words in the list of words.
Examples:
Input: S = { “the”, “quick”, “brown”, “fox”, “quick”}, word1 = “the”, word2 = “fox”
Output: 3
Explanation: Minimum distance between the words “the” and “fox” is 3
Input: S = {“geeks”, “for”, “geeks”, “contribute”, “practice”}, word1 = “geeks”, word2 = “practice”
Output: 2
Explanation: Minimum distance between the words “geeks” and “practice” is 2
Approach: Follow the steps to solve this problem:
- Initialize the variables d1 = -1, d2 = -1 and ans = INT_MAX.
- Traverse the string and check:
- If, s[i] is word1 then update d1 = i.
- If, s[i] is word2 then update d2 = i.
- If, d1 != -1 and d2 != -1, then update ans = min(ans, abs(d1-d2)).
- After traversing the string, return ans.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int shortestDistance(vector<string>& s,
string word1, string word2)
{
int d1 = -1, d2 = -1;
int ans = INT_MAX;
for ( int i = 0; i < s.size(); i++) {
if (s[i] == word1)
d1 = i;
if (s[i] == word2)
d2 = i;
if (d1 != -1 && d2 != -1)
ans = min(ans, abs (d1 - d2));
}
return ans;
}
int main()
{
vector<string> S
= { "the" , "quick" , "brown" , "fox" , "quick" };
string word1 = "the" , word2 = "fox" ;
cout << shortestDistance(S, word1, word2);
return 0;
}
|
C
#include <limits.h>
#include <stdio.h>
#include <string.h>
int shortestDistance( char s[][10], int n, char * word1,
char * word2)
{
int d1 = -1, d2 = -1;
int ans = INT_MAX;
for ( int i = 0; i < n; i++) {
if ( strcmp (s[i], word1) == 0)
d1 = i;
if ( strcmp (s[i], word2) == 0)
d2 = i;
if (d1 != -1 && d2 != -1)
ans = ans < abs (d1 - d2) ? ans : abs (d1 - d2);
}
return ans;
}
int main()
{
char S[][10]
= { "the" , "quick" , "brown" , "fox" , "quick" };
int n = 5;
char * word1 = "the" ;
char * word2 = "fox" ;
printf ( "%d" , shortestDistance(S, n, word1, word2));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int shortestDistance(String[] s,
String word1, String word2)
{
int d1 = - 1 , d2 = - 1 ;
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < s.length; i++) {
if (s[i] == word1)
d1 = i;
if (s[i] == word2)
d2 = i;
if (d1 != - 1 && d2 != - 1 )
ans = Math.min(ans, Math.abs(d1 - d2));
}
return ans;
}
public static void main (String[] args) {
String[] S
= { "the" , "quick" , "brown" , "fox" , "quick" };
String word1 = "the" , word2 = "fox" ;
System.out.println(shortestDistance(S, word1, word2));
}
}
|
Python3
def shortestDistance(s, word1, word2) :
d1 = - 1 ; d2 = - 1 ;
ans = 100000000 ;
for i in range ( len (s)) :
if (s[i] = = word1) :
d1 = i;
if (s[i] = = word2) :
d2 = i;
if (d1 ! = - 1 and d2 ! = - 1 ) :
ans = min (ans, abs (d1 - d2));
return ans;
if __name__ = = "__main__" :
S = [ "the" , "quick" , "brown" , "fox" , "quick" ];
word1 = "the" ; word2 = "fox" ;
print (shortestDistance(S, word1, word2));
|
C#
using System;
public class GFG {
static int shortestDistance( string [] s, string word1,
string word2)
{
int d1 = -1, d2 = -1;
int ans = Int32.MaxValue;
for ( int i = 0; i < s.Length; i++) {
if (s[i] == word1)
d1 = i;
if (s[i] == word2)
d2 = i;
if (d1 != -1 && d2 != -1)
ans = Math.Min(ans, Math.Abs(d1 - d2));
}
return ans;
}
static public void Main()
{
String[] S
= { "the" , "quick" , "brown" , "fox" , "quick" };
String word1 = "the" , word2 = "fox" ;
Console.WriteLine(
shortestDistance(S, word1, word2));
}
}
|
Javascript
function shortestDistance(s, word1, word2)
{
let d1 = -1, d2 = -1;
let ans = Number.MAX_VALUE;
for (let i = 0; i < s.length; i++) {
if (s[i] == word1)
d1 = i;
if (s[i] == word2)
d2 = i;
if (d1 != -1 && d2 != -1)
ans = Math.min(ans, Math.abs(d1 - d2));
}
return ans;
}
let S
= [ "the" , "quick" , "brown" , "fox" , "quick" ];
let word1 = "the" , word2 = "fox" ;
console.log(shortestDistance(S, word1, word2));
|
PHP
<?php
function shortestDistance( $s , $word1 , $word2 )
{
$d1 = -1;
$d2 = -1;
$ans = PHP_INT_MAX;
for ( $i = 0; $i < count ( $s ); $i ++) {
if ( $s [ $i ] == $word1 )
$d1 = $i ;
if ( $s [ $i ] == $word2 )
$d2 = $i ;
if ( $d1 != -1 && $d2 != -1)
$ans = min( $ans , abs ( $d1 - $d2 ));
}
return $ans ;
}
$S = array ( "the" , "quick" , "brown" , "fox" , "quick" );
$word1 = "the" ;
$word2 = "fox" ;
echo shortestDistance( $S , $word1 , $word2 );
?>
|
Time Complexity: O(N*L), where N is number of strings and L is size of maximum string.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...