8
8
* The Levenshtein distance is a measure of the similarity between two strings by calculating the minimum number of single-character
9
9
* edits (insertions, deletions, or substitutions) required to change one string into the other.
10
10
*/
11
- public class LevenshteinDistance {
11
+ public final class LevenshteinDistance {
12
12
13
13
/**
14
14
* Calculates the Levenshtein distance between two strings using a naive dynamic programming approach.
@@ -28,11 +28,11 @@ public class LevenshteinDistance {
28
28
* Note that this implementation uses a straightforward dynamic programming approach without any space optimization.
29
29
* It may consume more memory for larger input strings compared to the optimized version.
30
30
*/
31
- public static int naiveLevenshteinDistance (String string1 , String string2 ) {
31
+ public static int naiveLevenshteinDistance (final String string1 , final String string2 ) {
32
32
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 );
33
33
34
34
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 ;
36
36
distanceMatrix [i ][j ] = Math .min (distanceMatrix [i - 1 ][j - 1 ] + cost , Math .min (distanceMatrix [i ][j - 1 ] + 1 , distanceMatrix [i - 1 ][j ] + 1 ));
37
37
}));
38
38
@@ -57,7 +57,7 @@ public static int naiveLevenshteinDistance(String string1, String string2) {
57
57
*
58
58
* Additionally, it minimizes space usage by leveraging the shortest string horizontally and the longest string vertically in the computation matrix.
59
59
*/
60
- public static int optimizedLevenshteinDistance (String string1 , String string2 ) {
60
+ public static int optimizedLevenshteinDistance (final String string1 , final String string2 ) {
61
61
if (string1 .isEmpty ()) {
62
62
return string2 .length ();
63
63
}
@@ -69,9 +69,9 @@ public static int optimizedLevenshteinDistance(String string1, String string2) {
69
69
previousDistance [0 ] = j ;
70
70
71
71
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 ;
75
75
prevSubstitutionCost = previousDistance [i ];
76
76
previousDistance [i ] = Math .min (deletionCost , Math .min (insertionCost , substitutionCost ));
77
77
}
0 commit comments