Javascript Program

The edit distance between two strings refers to the minimum number of character insertions, deletions, and substitutions required to change one string to the other. For example, the edit distance between “kitten” and “sitting” is three: substitute the “k” for “s”, substitute the “e” for “i”, and append a “g”.

Given two strings, compute the edit distance between them.

function distance(str1,str2){
  let arr1 = str1.split("");
  let arr2 = str2.split("");
  let len1 = arr1.length;
  let len2 = arr2.length;
  let toCal = 0;
  if(len1  > len2 ){
    toCal = arr1.length;
  }else{
    toCal = arr2.length;
  }
  let subCount = 0;
  for(let i = 0; i<= toCal; i++ ){
      if(arr1[i] == arr2[i]){
      }else{
        subCount = subCount+1;
      }
  }
  return subCount;
}
console.log(distance("kitten","sitting"));
console.log(distance("kit","sitting"));
console.log(distance("kitten","sitting"));

Problem in Google Exam

Leave a comment