You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Find an efficient algorithm to find the smallest distance (measured in number of words) between any two given words in a string.
For example, given words "hello", and "world" and a text content of "dog cat hello cat dog dog hello cat world", return 1 because there's only one word "cat" in between the two words.
*/
functionfindAlgorithm(a,b,sentence){
// use case
if(a===b){
returnnull;
}
letgetWords=sentence.split(' ')
// The total length of the string is the minimum distance for now
letminDist=getWords.length+1;
// We will now start traversing through the entire string
for(letindex=0;index<getWords.length;index++){
if(getWords[index]==a){
for(letsindex=0;sindex<getWords.length;sindex++){
if(getWords[sindex]===b){
// We will define the distance as the index of the first word minus the index of the second word
letcurrEl=Math.abs(index-sindex)-1;
// we will compare the current distance between the previously assumed
if(currEl<minDist){
minDist=currEl;
}
}
}
}
}
returnminDist;
}
console.log(findAlgorithm('hello','world','dog cat hello cat dog hello cat world'));