Skip to content

Commit 5d39af3

Browse files
deep20jainpivovarit
authored andcommitted
BAEL 1143 - Edit Distance - deep20jain@gmail.com (eugenp#2718)
* Calculate edit distance * Fixing formatting * Making variable local to method
1 parent f4c3469 commit 5d39af3

5 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.algorithms.editdistance;
2+
3+
public class EditDistanceBase {
4+
5+
public static int costOfSubstitution(char a, char b) {
6+
if (a == b) {
7+
return 0;
8+
}
9+
return 1;
10+
}
11+
12+
public static int min(int... numbers) {
13+
int min = Integer.MAX_VALUE;
14+
15+
for (int x : numbers) {
16+
if (x < min)
17+
min = x;
18+
}
19+
20+
return min;
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.algorithms.editdistance;
2+
3+
public class EditDistanceDynamicProgramming extends EditDistanceBase {
4+
5+
public static int calculate(String x, String y) {
6+
int[][] dp = new int[x.length() + 1][y.length() + 1];
7+
8+
for (int i = 0; i <= x.length(); i++) {
9+
for (int j = 0; j <= y.length(); j++) {
10+
if (i == 0)
11+
dp[i][j] = j;
12+
13+
else if (j == 0)
14+
dp[i][j] = i;
15+
16+
else {
17+
dp[i][j] = min(dp[i - 1][j - 1] + costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)), dp[i - 1][j] + 1, dp[i][j - 1] + 1);
18+
}
19+
}
20+
}
21+
22+
return dp[x.length()][y.length()];
23+
}
24+
25+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.algorithms.editdistance;
2+
3+
public class EditDistanceRecursive extends EditDistanceBase {
4+
5+
public static int calculate(String x, String y) {
6+
7+
if (x.isEmpty())
8+
return y.length();
9+
10+
if (y.isEmpty())
11+
return x.length();
12+
13+
int substitution = calculate(x.substring(1), y.substring(1)) + costOfSubstitution(x.charAt(0), y.charAt(0));
14+
int insertion = calculate(x, y.substring(1)) + 1;
15+
int deletion = calculate(x.substring(1), y) + 1;
16+
17+
return min(substitution, insertion, deletion);
18+
}
19+
20+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.algorithms.editdistance;
2+
3+
import org.junit.runners.Parameterized.Parameters;
4+
5+
import java.util.Arrays;
6+
import java.util.Collection;
7+
8+
public class EditDistanceDataProvider {
9+
10+
@Parameters
11+
public static Collection<Object[]> getLists() {
12+
return Arrays.asList(new Object[][] {
13+
{ "", "", 0 },
14+
{ "ago", "", 3 },
15+
{ "", "do", 2 },
16+
{ "abc", "adc", 1 },
17+
{ "peek", "pesek", 1 },
18+
{ "sunday", "saturday", 3 }
19+
});
20+
}
21+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung.algorithms.editdistance;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.junit.runners.Parameterized;
7+
8+
@RunWith(Parameterized.class)
9+
public class EditDistanceTest extends EditDistanceDataProvider {
10+
11+
String x;
12+
String y;
13+
int result;
14+
15+
public EditDistanceTest(String a, String b, int res) {
16+
super();
17+
x = a;
18+
y = b;
19+
result = res;
20+
}
21+
22+
@Test
23+
public void testEditDistance_RecursiveImplementation() {
24+
Assert.assertEquals(result, EditDistanceRecursive.calculate(x, y));
25+
}
26+
27+
@Test
28+
public void testEditDistance_givenDynamicProgrammingImplementation() {
29+
Assert.assertEquals(result, EditDistanceDynamicProgramming.calculate(x, y));
30+
}
31+
}

0 commit comments

Comments
 (0)