Skip to content

Commit be979e3

Browse files
committed
ref: make LevenshteinDistance class a proper utility
1 parent 488f1c5 commit be979e3

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

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

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* The Levenshtein distance is a measure of the similarity between two strings by calculating the minimum number of single-character
99
* edits (insertions, deletions, or substitutions) required to change one string into the other.
1010
*/
11-
public class LevenshteinDistance {
11+
public final class LevenshteinDistance {
1212

1313
/**
1414
* Calculates the Levenshtein distance between two strings using a naive dynamic programming approach.
@@ -28,11 +28,11 @@ public class LevenshteinDistance {
2828
* Note that this implementation uses a straightforward dynamic programming approach without any space optimization.
2929
* It may consume more memory for larger input strings compared to the optimized version.
3030
*/
31-
public static int naiveLevenshteinDistance(String string1, String string2) {
31+
public static int naiveLevenshteinDistance(final String string1, final String string2) {
3232
int[][] distanceMatrix = IntStream.rangeClosed(0, string1.length()).mapToObj(i -> IntStream.rangeClosed(0, string2.length()).map(j -> (i == 0) ? j : (j == 0) ? i : 0).toArray()).toArray(int[][] ::new);
3333

3434
IntStream.range(1, string1.length() + 1).forEach(i -> IntStream.range(1, string2.length() + 1).forEach(j -> {
35-
int cost = (string1.charAt(i - 1) == string2.charAt(j - 1)) ? 0 : 1;
35+
final int cost = (string1.charAt(i - 1) == string2.charAt(j - 1)) ? 0 : 1;
3636
distanceMatrix[i][j] = Math.min(distanceMatrix[i - 1][j - 1] + cost, Math.min(distanceMatrix[i][j - 1] + 1, distanceMatrix[i - 1][j] + 1));
3737
}));
3838

@@ -57,7 +57,7 @@ public static int naiveLevenshteinDistance(String string1, String string2) {
5757
*
5858
* Additionally, it minimizes space usage by leveraging the shortest string horizontally and the longest string vertically in the computation matrix.
5959
*/
60-
public static int optimizedLevenshteinDistance(String string1, String string2) {
60+
public static int optimizedLevenshteinDistance(final String string1, final String string2) {
6161
if (string1.isEmpty()) {
6262
return string2.length();
6363
}
@@ -69,9 +69,9 @@ public static int optimizedLevenshteinDistance(String string1, String string2) {
6969
previousDistance[0] = j;
7070

7171
for (int i = 1; i <= string1.length(); i++) {
72-
int deletionCost = previousDistance[i] + 1;
73-
int insertionCost = previousDistance[i - 1] + 1;
74-
int substitutionCost = (string1.charAt(i - 1) == string2.charAt(j - 1)) ? prevSubstitutionCost : prevSubstitutionCost + 1;
72+
final int deletionCost = previousDistance[i] + 1;
73+
final int insertionCost = previousDistance[i - 1] + 1;
74+
final int substitutionCost = (string1.charAt(i - 1) == string2.charAt(j - 1)) ? prevSubstitutionCost : prevSubstitutionCost + 1;
7575
prevSubstitutionCost = previousDistance[i];
7676
previousDistance[i] = Math.min(deletionCost, Math.min(insertionCost, substitutionCost));
7777
}

0 commit comments

Comments
 (0)