Skip to content

Commit 35e2d66

Browse files
fix build
1 parent 315090b commit 35e2d66

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

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

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,35 @@ public int minDistance(String word1, String word2) {
3737
return table[m][n];
3838
}
3939
}
40-
public static class Solution2{
40+
41+
public static class Solution2 {
4142
public int minDistance(String word1, String word2) {
4243
// using levenshtein Distance to find minimum transformation operations required from word 1 to word 2
4344
int[][] dp = new int[word1.length()][word2.length()];
4445
// fill the dp array with -1 value
45-
for(int [] rows : dp){
46+
for (int[] rows : dp) {
4647
Arrays.fill(rows, -1);
4748
}
48-
return levenshteinDistance(word1, word1.length()-1 , word2, word2.length()-1, dp);
49+
return levenshteinDistance(word1, word1.length() - 1, word2, word2.length() - 1, dp);
4950
}
50-
private int levenshteinDistance( String s1, int s1Index, String s2, int s2Index, int[][] dp){
5151

52-
if(s1Index < 0){ // when s1 is "" perform all insertions to get s1 to s2
52+
private int levenshteinDistance(String s1, int s1Index, String s2, int s2Index, int[][] dp) {
53+
54+
if (s1Index < 0) { // when s1 is "" perform all insertions to get s1 to s2
5355
return s2Index + 1;
54-
}
55-
else if(s2Index < 0) { // when s2 is "" perform all deletions from s1
56+
} else if (s2Index < 0) { // when s2 is "" perform all deletions from s1
5657
return s1Index + 1;
5758
}
5859

5960
// base condition when dp array is filled, return the distance
60-
if(dp[s1Index][s2Index] != -1)
61+
if (dp[s1Index][s2Index] != -1) {
6162
return dp[s1Index][s2Index];
63+
}
6264

63-
if(s1.charAt(s1Index) == s2.charAt(s2Index)){
65+
if (s1.charAt(s1Index) == s2.charAt(s2Index)) {
6466
// Characters match, no edit distance to be calculated
6567
dp[s1Index][s2Index] = levenshteinDistance(s1, s1Index - 1, s2, s2Index - 1, dp);
66-
}
67-
else{
68+
} else {
6869
// When there is a character mismatch, perform operations
6970

7071
// Insertion

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public class _72Test {
1313

1414
@BeforeClass
1515
public static void setup() {
16-
solution1 = new _72.Solution1(); solution2 = new _72.Solution2();
16+
solution1 = new _72.Solution1();
17+
solution2 = new _72.Solution2();
1718
}
1819

1920
@Test
@@ -25,4 +26,14 @@ public void test1() {
2526
public void test2() {
2627
assertEquals(5, solution1.minDistance("Ashmi", "Chheda"));
2728
}
29+
30+
@Test
31+
public void test3() {
32+
assertEquals(1, solution2.minDistance("Ada", "Adam"));
33+
}
34+
35+
@Test
36+
public void test4() {
37+
assertEquals(5, solution2.minDistance("Ashmi", "Chheda"));
38+
}
2839
}

0 commit comments

Comments
 (0)