|
| 1 | +package Dynamic_Programming.Longest_Common_SubString; |
| 2 | + |
| 3 | +// Problem Statement: Edit Distance |
| 4 | +// LeetCode Question: 72. Edit Distance |
| 5 | + |
| 6 | +public class Problem_12_Edit_Distance { |
| 7 | + // Brute Force Approach |
| 8 | + public int findMinOperations(String s1, String s2) { |
| 9 | + return findMinOperationsRecursive(s1, s2, 0, 0); |
| 10 | + } |
| 11 | + |
| 12 | + private int findMinOperationsRecursive(String s1, String s2, int i1, int i2) { |
| 13 | + |
| 14 | + // if we have reached the end of s1, then we have to insert all the remaining characters of s2 |
| 15 | + if(i1 == s1.length()) |
| 16 | + return s2.length() - i2; |
| 17 | + |
| 18 | + // if we have reached the end of s2, then we have to delete all the remaining characters of s1 |
| 19 | + if(i2 == s2.length()) |
| 20 | + return s1.length() - i1; |
| 21 | + |
| 22 | + // If the strings have a matching character, we can recursively match for the remaining lengths. |
| 23 | + if(s1.charAt(i1) == s2.charAt(i2)) |
| 24 | + return findMinOperationsRecursive(s1, s2, i1+1, i2+1); |
| 25 | + |
| 26 | + int c1 = 1 + findMinOperationsRecursive(s1, s2, i1+1, i2); //perform deletion |
| 27 | + int c2 = 1 + findMinOperationsRecursive(s1, s2, i1, i2+1); //perform insertion |
| 28 | + int c3 = 1 + findMinOperationsRecursive(s1, s2, i1+1, i2+1); // perform replacement |
| 29 | + |
| 30 | + return Math.min(c1, Math.min(c2, c3)); |
| 31 | + } |
| 32 | + |
| 33 | + // Top-down Dynamic Programming with Memoization |
| 34 | + public int findMinOperations_1(String s1, String s2) { |
| 35 | + Integer[][] dp = new Integer[s1.length()+1][s2.length()+1]; |
| 36 | + return findMinOperationsRecursive(dp, s1, s2, 0, 0); |
| 37 | + } |
| 38 | + |
| 39 | + private int findMinOperationsRecursive(Integer[][] dp, String s1, String s2, int i1, int i2) { |
| 40 | + |
| 41 | + if(dp[i1][i2] == null) { |
| 42 | + // if we have reached the end of s1, then we have to insert all the remaining characters of s2 |
| 43 | + if(i1 == s1.length()) |
| 44 | + dp[i1][i2] = s2.length() - i2; |
| 45 | + |
| 46 | + // if we have reached the end of s2, then we have to delete all the remaining characters of s1 |
| 47 | + else if(i2 == s2.length()) |
| 48 | + dp[i1][i2] = s1.length() - i1; |
| 49 | + |
| 50 | + // If the strings have a matching character, we can recursively match for the remaining lengths |
| 51 | + else if(s1.charAt(i1) == s2.charAt(i2)) |
| 52 | + dp[i1][i2] = findMinOperationsRecursive(dp, s1, s2, i1+1, i2+1); |
| 53 | + else { |
| 54 | + int c1 = findMinOperationsRecursive(dp, s1, s2, i1+1, i2); //delete |
| 55 | + int c2 = findMinOperationsRecursive(dp, s1, s2, i1, i2+1); //insert |
| 56 | + int c3 = findMinOperationsRecursive(dp, s1, s2, i1+1, i2+1); //replace |
| 57 | + dp[i1][i2] = 1 + Math.min(c1, Math.min(c2, c3)); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + return dp[i1][i2]; |
| 62 | + } |
| 63 | + |
| 64 | + // Bottom-up Dynamic Programming |
| 65 | + public int findMinOperations_3(String s1, String s2) { |
| 66 | + int[][] dp = new int[s1.length()+1][s2.length()+1]; |
| 67 | + |
| 68 | + // if s2 is empty, we can remove all the characters of s1 to make it empty too |
| 69 | + for(int i1=0; i1 <= s1.length(); i1++) |
| 70 | + dp[i1][0] = i1; |
| 71 | + |
| 72 | + // if s1 is empty, we have to insert all the characters of s2 |
| 73 | + for(int i2=0; i2 <= s2.length(); i2++) |
| 74 | + dp[0][i2] = i2; |
| 75 | + |
| 76 | + for(int i1=1; i1 <= s1.length(); i1++) { |
| 77 | + for(int i2=1; i2 <= s2.length(); i2++) { |
| 78 | + // If the strings have a matching character, we can recursively match for the remaining lengths |
| 79 | + if(s1.charAt(i1-1) == s2.charAt(i2-1)) |
| 80 | + dp[i1][i2] = dp[i1-1][i2-1]; |
| 81 | + else |
| 82 | + dp[i1][i2] = 1 + Math.min(dp[i1-1][i2], //delete |
| 83 | + Math.min(dp[i1][i2-1], //insert |
| 84 | + dp[i1-1][i2-1])); //replace |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return dp[s1.length()][s2.length()]; |
| 89 | + } |
| 90 | +} |
0 commit comments