Skip to content

Commit 315090b

Browse files
authored
added dynamic programming top down apprach for 72 with explanation (#134)
1 parent d948bb6 commit 315090b

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/main/java/com/fishercoder/solutions/_72.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.Arrays;
4+
35
public class _72 {
46

57
public static class Solution1 {
@@ -35,4 +37,45 @@ public int minDistance(String word1, String word2) {
3537
return table[m][n];
3638
}
3739
}
40+
public static class Solution2{
41+
public int minDistance(String word1, String word2) {
42+
// using levenshtein Distance to find minimum transformation operations required from word 1 to word 2
43+
int[][] dp = new int[word1.length()][word2.length()];
44+
// fill the dp array with -1 value
45+
for(int [] rows : dp){
46+
Arrays.fill(rows, -1);
47+
}
48+
return levenshteinDistance(word1, word1.length()-1 , word2, word2.length()-1, dp);
49+
}
50+
private int levenshteinDistance( String s1, int s1Index, String s2, int s2Index, int[][] dp){
51+
52+
if(s1Index < 0){ // when s1 is "" perform all insertions to get s1 to s2
53+
return s2Index + 1;
54+
}
55+
else if(s2Index < 0) { // when s2 is "" perform all deletions from s1
56+
return s1Index + 1;
57+
}
58+
59+
// base condition when dp array is filled, return the distance
60+
if(dp[s1Index][s2Index] != -1)
61+
return dp[s1Index][s2Index];
62+
63+
if(s1.charAt(s1Index) == s2.charAt(s2Index)){
64+
// Characters match, no edit distance to be calculated
65+
dp[s1Index][s2Index] = levenshteinDistance(s1, s1Index - 1, s2, s2Index - 1, dp);
66+
}
67+
else{
68+
// When there is a character mismatch, perform operations
69+
70+
// Insertion
71+
int insertValue = levenshteinDistance(s1, s1Index, s2, s2Index - 1, dp);
72+
int deleteValue = levenshteinDistance(s1, s1Index - 1, s2, s2Index, dp);
73+
int substituteValue = levenshteinDistance(s1, s1Index - 1, s2, s2Index - 1, dp);
74+
75+
/* Now we need to take the minimum of the 3 operations to find the edit distance and add 1 to the min cost action denoting that an action is performed */
76+
dp[s1Index][s2Index] = 1 + Math.min(insertValue, Math.min(deleteValue, substituteValue));
77+
}
78+
return dp[s1Index][s2Index];
79+
}
80+
}
3881
}

src/test/java/com/fishercoder/_72Test.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,21 @@
88

99
public class _72Test {
1010
private static _72.Solution1 solution1;
11+
private static _72.Solution2 solution2;
12+
1113

1214
@BeforeClass
1315
public static void setup() {
14-
solution1 = new _72.Solution1();
16+
solution1 = new _72.Solution1(); solution2 = new _72.Solution2();
1517
}
1618

1719
@Test
1820
public void test1() {
1921
assertEquals(1, solution1.minDistance("Ada", "Adam"));
2022
}
23+
24+
@Test
25+
public void test2() {
26+
assertEquals(5, solution1.minDistance("Ashmi", "Chheda"));
27+
}
2128
}

0 commit comments

Comments
 (0)