EditDistance refactor (#2853)
This commit is contained in:
parent
a636f754b0
commit
1da6e1df42
|
@ -1,22 +1,15 @@
|
|||
package com.baeldung.algorithms.editdistance;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class EditDistanceBase {
|
||||
|
||||
public static int costOfSubstitution(char a, char b) {
|
||||
if (a == b) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
static int costOfSubstitution(char a, char b) {
|
||||
return a == b ? 0 : 1;
|
||||
}
|
||||
|
||||
public static int min(int... numbers) {
|
||||
int min = Integer.MAX_VALUE;
|
||||
|
||||
for (int x : numbers) {
|
||||
if (x < min)
|
||||
min = x;
|
||||
}
|
||||
|
||||
return min;
|
||||
static int min(int... numbers) {
|
||||
return Arrays.stream(numbers)
|
||||
.min().orElse(Integer.MAX_VALUE);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package com.baeldung.algorithms.editdistance;
|
|||
|
||||
public class EditDistanceDynamicProgramming extends EditDistanceBase {
|
||||
|
||||
public static int calculate(String x, String y) {
|
||||
static int calculate(String x, String y) {
|
||||
int[][] dp = new int[x.length() + 1][y.length() + 1];
|
||||
|
||||
for (int i = 0; i <= x.length(); i++) {
|
||||
|
@ -14,12 +14,13 @@ public class EditDistanceDynamicProgramming extends EditDistanceBase {
|
|||
dp[i][j] = i;
|
||||
|
||||
else {
|
||||
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);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dp[x.length()][y.length()];
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,13 +2,15 @@ package com.baeldung.algorithms.editdistance;
|
|||
|
||||
public class EditDistanceRecursive extends EditDistanceBase {
|
||||
|
||||
public static int calculate(String x, String y) {
|
||||
static int calculate(String x, String y) {
|
||||
|
||||
if (x.isEmpty())
|
||||
if (x.isEmpty()) {
|
||||
return y.length();
|
||||
}
|
||||
|
||||
if (y.isEmpty())
|
||||
if (y.isEmpty()) {
|
||||
return x.length();
|
||||
}
|
||||
|
||||
int substitution = calculate(x.substring(1), y.substring(1)) + costOfSubstitution(x.charAt(0), y.charAt(0));
|
||||
int insertion = calculate(x, y.substring(1)) + 1;
|
||||
|
@ -16,5 +18,4 @@ public class EditDistanceRecursive extends EditDistanceBase {
|
|||
|
||||
return min(substitution, insertion, deletion);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,16 +1,17 @@
|
|||
package com.baeldung.algorithms.editdistance;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class EditDistanceTest extends EditDistanceDataProvider {
|
||||
|
||||
String x;
|
||||
String y;
|
||||
int result;
|
||||
private String x;
|
||||
private String y;
|
||||
private int result;
|
||||
|
||||
public EditDistanceTest(String a, String b, int res) {
|
||||
super();
|
||||
|
@ -21,11 +22,11 @@ public class EditDistanceTest extends EditDistanceDataProvider {
|
|||
|
||||
@Test
|
||||
public void testEditDistance_RecursiveImplementation() {
|
||||
Assert.assertEquals(result, EditDistanceRecursive.calculate(x, y));
|
||||
assertEquals(result, EditDistanceRecursive.calculate(x, y));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEditDistance_givenDynamicProgrammingImplementation() {
|
||||
Assert.assertEquals(result, EditDistanceDynamicProgramming.calculate(x, y));
|
||||
assertEquals(result, EditDistanceDynamicProgramming.calculate(x, y));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue