Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Javascript program to
- // find minimum numberoperations
- function editDist(str1, str2, m, n)
- {
- // If first string is empty, the
- // only option is to insert all
- // characters of second string into first
- if (m == 0) {
- return n ;
- }
- // If second string is empty, the only
- // option is to remove all characters
- // of first string
- if (n == 0) {
- return m;
- }
- // If last characters of two strings are
- // same, nothing much to do. Ignore last
- // characters and get count for remaining
- // strings.
- if (str1[m - 1] == str2[n - 1]) {
- return editDist(str1, str2, m - 1, n - 1);
- }
- // If last characters are not same, consider all
- // three operations on last character of first
- // string, recursively compute minimum cost for all
- // three operations and take minimum of three
- // values.
- var cI = editDist(str1, str2, m, n - 1); // Insert
- var cR = editDist(str1, str2, m - 1, n); // Remove
- var cC = editDist(str1, str2, m - 1, n - 1); // Replace
- var minC = Math.min(cI, cR, cC);
- return initialCost + minC;
- }
- // Driver code
- let str1 = "developer";
- let str2 = "enveloped";
- var initialCost = 1;
- debugger;
- var cost = editDist(str1, str2, str1.length, str2.length);
- console.log(cost);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement