Skip to content

Commit e0b1235

Browse files
authored
Fix ArrayIndexOutOfBoundsException in LevenshteinDistance (TheAlgorithms#3871)
1 parent 69a4284 commit e0b1235

2 files changed

Lines changed: 40 additions & 26 deletions

File tree

src/main/java/com/thealgorithms/dynamicprogramming/LevenshteinDistance.java

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,40 @@ private static int minimum(int a, int b, int c) {
1717
}
1818
}
1919

20-
private static int calculate_distance(String a, String b) {
21-
int len_a = a.length() + 1;
22-
int len_b = b.length() + 1;
23-
int[][] distance_mat = new int[len_a][len_b];
24-
for (int i = 0; i < len_a; i++) {
25-
distance_mat[i][0] = i;
20+
public static int calculateLevenshteinDistance(String str1, String str2) {
21+
int len1 = str1.length() + 1;
22+
int len2 = str2.length() + 1;
23+
int[][] distanceMat = new int[len1][len2];
24+
for (int i = 0; i < len1; i++) {
25+
distanceMat[i][0] = i;
2626
}
27-
for (int j = 0; j < len_b; j++) {
28-
distance_mat[0][j] = j;
27+
for (int j = 0; j < len2; j++) {
28+
distanceMat[0][j] = j;
2929
}
30-
for (int i = 0; i < len_a; i++) {
31-
for (int j = 0; j < len_b; j++) {
32-
int cost;
33-
if (a.charAt(i) == b.charAt(j)) {
34-
cost = 0;
30+
for (int i = 1; i < len1; i++) {
31+
for (int j = 1; j < len2; j++) {
32+
if (str1.charAt(i - 1) == str2.charAt(j - 1)) {
33+
distanceMat[i][j] = distanceMat[i - 1][j - 1];
3534
} else {
36-
cost = 1;
35+
distanceMat[i][j] =
36+
1 + minimum(
37+
distanceMat[i - 1][j],
38+
distanceMat[i - 1][j - 1],
39+
distanceMat[i][j - 1]
40+
);
3741
}
38-
distance_mat[i][j] =
39-
minimum(
40-
distance_mat[i - 1][j],
41-
distance_mat[i - 1][j - 1],
42-
distance_mat[i][j - 1]
43-
) +
44-
cost;
4542
}
4643
}
47-
return distance_mat[len_a - 1][len_b - 1];
44+
return distanceMat[len1 - 1][len2 - 1];
4845
}
4946

5047
public static void main(String[] args) {
51-
String a = ""; // enter your string here
52-
String b = ""; // enter your string here
48+
String str1 = ""; // enter your string here
49+
String str2 = ""; // enter your string here
5350

5451
System.out.print(
55-
"Levenshtein distance between " + a + " and " + b + " is: "
52+
"Levenshtein distance between " + str1 + " and " + str2 + " is: "
5653
);
57-
System.out.println(calculate_distance(a, b));
54+
System.out.println(calculateLevenshteinDistance(str1, str2));
5855
}
5956
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.thealgorithms.dynamicprogramming;
2+
3+
import org.junit.jupiter.params.ParameterizedTest;
4+
import org.junit.jupiter.params.provider.CsvSource;
5+
6+
import static org.junit.jupiter.api.Assertions.assertEquals;
7+
8+
public class LevenshteinDistanceTests {
9+
10+
@ParameterizedTest
11+
@CsvSource({"dog,cat,3", "sunday,saturday,3", "cat,cats,1", "rain,train,1"})
12+
void levenshteinDistanceTest(String str1, String str2, int distance) {
13+
int result = LevenshteinDistance.calculateLevenshteinDistance(str1, str2);
14+
assertEquals(distance, result);
15+
}
16+
17+
}

0 commit comments

Comments
 (0)