Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit315090b

Browse files
authored
added dynamic programming top down apprach for 72 with explanation (#134)
1 parentd948bb6 commit315090b

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
packagecom.fishercoder.solutions;
22

3+
importjava.util.Arrays;
4+
35
publicclass_72 {
46

57
publicstaticclassSolution1 {
@@ -35,4 +37,45 @@ public int minDistance(String word1, String word2) {
3537
returntable[m][n];
3638
}
3739
}
40+
publicstaticclassSolution2{
41+
publicintminDistance(Stringword1,Stringword2) {
42+
// using levenshtein Distance to find minimum transformation operations required from word 1 to word 2
43+
int[][]dp =newint[word1.length()][word2.length()];
44+
// fill the dp array with -1 value
45+
for(int []rows :dp){
46+
Arrays.fill(rows, -1);
47+
}
48+
returnlevenshteinDistance(word1,word1.length()-1 ,word2,word2.length()-1,dp);
49+
}
50+
privateintlevenshteinDistance(Strings1,ints1Index,Strings2,ints2Index,int[][]dp){
51+
52+
if(s1Index <0){// when s1 is "" perform all insertions to get s1 to s2
53+
returns2Index +1;
54+
}
55+
elseif(s2Index <0) {// when s2 is "" perform all deletions from s1
56+
returns1Index +1;
57+
}
58+
59+
// base condition when dp array is filled, return the distance
60+
if(dp[s1Index][s2Index] != -1)
61+
returndp[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+
intinsertValue =levenshteinDistance(s1,s1Index,s2,s2Index -1,dp);
72+
intdeleteValue =levenshteinDistance(s1,s1Index -1,s2,s2Index,dp);
73+
intsubstituteValue =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+
returndp[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
publicclass_72Test {
1010
privatestatic_72.Solution1solution1;
11+
privatestatic_72.Solution2solution2;
12+
1113

1214
@BeforeClass
1315
publicstaticvoidsetup() {
14-
solution1 =new_72.Solution1();
16+
solution1 =new_72.Solution1();solution2 =new_72.Solution2();
1517
}
1618

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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp