Guest User

Untitled

a guest
Mar 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. function min(x, y, z){
  12. if (x<=y && x<=z) return x;
  13. if (y<=x && y<=z) return y;
  14. else return z;
  15. }
  16.  
  17. function editDist(str1, str2, m, n){
  18.  
  19. console.log(' top ', m, n);
  20. // Create a table to store results of subproblems
  21. const dp = {};
  22.  
  23. // Fill d[][] in bottom up manner
  24. for (let i=0; i<=m; i++)
  25. {
  26. for (let j=0; j<=n; j++)
  27. {
  28. // If first string is empty, only option is to
  29. // isnert all characters of second string
  30. if (i==0)
  31. dp[i+'_'+j] = j; // Min. operations = j
  32.  
  33. // If second string is empty, only option is to
  34. // remove all characters of second string
  35. else if (j==0)
  36. dp[i+'_'+j] = i; // Min. operations = i
  37.  
  38. // If last characters are same, ignore last char
  39. // and recur for remaining string
  40. else if (str1[i-1] == str2[j-1])
  41. dp[i+'_'+j] = dp[(i-1)+'_'+(j-1)];
  42.  
  43. // If last character are different, consider all
  44. // possibilities and find minimum
  45. else
  46. dp[i+'_'+j] = 1 + min(dp[i+'_'+(j-1)], // Insert
  47. dp[(i-1)+'_'+j], // Remove
  48. dp[(i-1)+'_'+(j-1)]); // Replace
  49. }
  50. }
  51.  
  52. console.log(' final ', dp);
  53. return dp[m+'_'+n];
  54.  
  55. }
  56.  
  57. let str1 = "ab", str2 = "ba";
  58.  
  59. console.log(' finding minimum streps ==');
  60.  
  61. console.log(' minimum steps ', editDist(str1, str2, str1.length, str2.length));
  62. </script>
  63.  
  64.  
  65.  
  66. <script id="jsbin-source-javascript" type="text/javascript">function min(x, y, z){
  67. if (x<=y && x<=z) return x;
  68. if (y<=x && y<=z) return y;
  69. else return z;
  70. }
  71.  
  72. function editDist(str1, str2, m, n){
  73.  
  74. console.log(' top ', m, n);
  75. // Create a table to store results of subproblems
  76. const dp = {};
  77.  
  78. // Fill d[][] in bottom up manner
  79. for (let i=0; i<=m; i++)
  80. {
  81. for (let j=0; j<=n; j++)
  82. {
  83. // If first string is empty, only option is to
  84. // isnert all characters of second string
  85. if (i==0)
  86. dp[i+'_'+j] = j; // Min. operations = j
  87.  
  88. // If second string is empty, only option is to
  89. // remove all characters of second string
  90. else if (j==0)
  91. dp[i+'_'+j] = i; // Min. operations = i
  92.  
  93. // If last characters are same, ignore last char
  94. // and recur for remaining string
  95. else if (str1[i-1] == str2[j-1])
  96. dp[i+'_'+j] = dp[(i-1)+'_'+(j-1)];
  97.  
  98. // If last character are different, consider all
  99. // possibilities and find minimum
  100. else
  101. dp[i+'_'+j] = 1 + min(dp[i+'_'+(j-1)], // Insert
  102. dp[(i-1)+'_'+j], // Remove
  103. dp[(i-1)+'_'+(j-1)]); // Replace
  104. }
  105. }
  106.  
  107. console.log(' final ', dp);
  108. return dp[m+'_'+n];
  109.  
  110. }
  111.  
  112. let str1 = "ab", str2 = "ba";
  113.  
  114. console.log(' finding minimum streps ==');
  115.  
  116. console.log(' minimum steps ', editDist(str1, str2, str1.length, str2.length));
  117.  
  118.  
  119.  
  120.  
  121. </script></body>
  122. </html>
Add Comment
Please, Sign In to add comment