Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- function min(x, y, z){
- if (x<=y && x<=z) return x;
- if (y<=x && y<=z) return y;
- else return z;
- }
- function editDist(str1, str2, m, n){
- console.log(' top ', m, n);
- // Create a table to store results of subproblems
- const dp = {};
- // Fill d[][] in bottom up manner
- for (let i=0; i<=m; i++)
- {
- for (let j=0; j<=n; j++)
- {
- // If first string is empty, only option is to
- // isnert all characters of second string
- if (i==0)
- dp[i+'_'+j] = j; // Min. operations = j
- // If second string is empty, only option is to
- // remove all characters of second string
- else if (j==0)
- dp[i+'_'+j] = i; // Min. operations = i
- // If last characters are same, ignore last char
- // and recur for remaining string
- else if (str1[i-1] == str2[j-1])
- dp[i+'_'+j] = dp[(i-1)+'_'+(j-1)];
- // If last character are different, consider all
- // possibilities and find minimum
- else
- dp[i+'_'+j] = 1 + min(dp[i+'_'+(j-1)], // Insert
- dp[(i-1)+'_'+j], // Remove
- dp[(i-1)+'_'+(j-1)]); // Replace
- }
- }
- console.log(' final ', dp);
- return dp[m+'_'+n];
- }
- let str1 = "ab", str2 = "ba";
- console.log(' finding minimum streps ==');
- console.log(' minimum steps ', editDist(str1, str2, str1.length, str2.length));
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">function min(x, y, z){
- if (x<=y && x<=z) return x;
- if (y<=x && y<=z) return y;
- else return z;
- }
- function editDist(str1, str2, m, n){
- console.log(' top ', m, n);
- // Create a table to store results of subproblems
- const dp = {};
- // Fill d[][] in bottom up manner
- for (let i=0; i<=m; i++)
- {
- for (let j=0; j<=n; j++)
- {
- // If first string is empty, only option is to
- // isnert all characters of second string
- if (i==0)
- dp[i+'_'+j] = j; // Min. operations = j
- // If second string is empty, only option is to
- // remove all characters of second string
- else if (j==0)
- dp[i+'_'+j] = i; // Min. operations = i
- // If last characters are same, ignore last char
- // and recur for remaining string
- else if (str1[i-1] == str2[j-1])
- dp[i+'_'+j] = dp[(i-1)+'_'+(j-1)];
- // If last character are different, consider all
- // possibilities and find minimum
- else
- dp[i+'_'+j] = 1 + min(dp[i+'_'+(j-1)], // Insert
- dp[(i-1)+'_'+j], // Remove
- dp[(i-1)+'_'+(j-1)]); // Replace
- }
- }
- console.log(' final ', dp);
- return dp[m+'_'+n];
- }
- let str1 = "ab", str2 = "ba";
- console.log(' finding minimum streps ==');
- console.log(' minimum steps ', editDist(str1, str2, str1.length, str2.length));
- </script></body>
- </html>
Add Comment
Please, Sign In to add comment