JavaScript Related Questions: 1. Given two strings S and T, determine if they ar
ID: 3743985 • Letter: J
Question
JavaScript Related Questions:
1. Given two strings S and T, determine if they are both one edit distance apart. And explain.
2.
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list. And explain.
For example,
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
Given word1 = “coding”, word2 = “practice”, return 3.
Given word1 = "makes", word2 = "coding", return 1.
Note:
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
Explanation / Answer
Distance between two strings means:
suppose if e take two strings s="near" and t="fear" , now the charecters are to be replaced charecter by charecter so that the second word is to be formed.here ' n ' is replaced with ' f ' and the replacements done are "one" so the distance is one.
1.function isOneEditDistance(var s, var t)
{
if(s==null || t==null)
return false;
var m = s.length();
var n = t.length();
if(Math.abs(m-n)>1){
return false;
}
var i=0;
var j=0;
var count=0;
while(i<m&&j<n){
if(s.charAt(i)==t.charAt(j)){
i++;
j++;
}else{
count++;
if(count>1)
return false;
if(m>n){
i++;
}else if(m<n){
j++;
}else{
i++;
j++;
}
}
}
if(i<m||j<n){
count++;
}
if(count==1)
return true;
return false;
}
2.function findMinimumWordDistance(words, wordA, wordB) {
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.