@@ -27,20 +27,20 @@ public static String findClosest(String input, Collection<String> candidates) {
2727 String best = null ;
2828 int bestDistance = Integer .MAX_VALUE ;
2929
30- for (String cand : candidates ) {
31- if (cand == null || cand .isEmpty ()) continue ;
32- if (cand .equalsIgnoreCase (input )) {
30+ for (String candidate : candidates ) {
31+ if (candidate == null || candidate .isEmpty ()) continue ;
32+ if (candidate .equalsIgnoreCase (input )) {
3333 // exact match ignoring case - return immediately
34- return cand ;
34+ return candidate ;
3535 }
36- int dist = levenshteinDistance (input .toLowerCase (), cand .toLowerCase ());
37- if (dist < bestDistance ) {
38- bestDistance = dist ;
39- best = cand ;
36+ int distance = levenshteinDistance (input .toLowerCase (), candidate .toLowerCase ());
37+ if (distance < bestDistance ) {
38+ bestDistance = distance ;
39+ best = candidate ;
4040 }
4141 }
4242
43- // Choose a threshold: allow suggestion only when distance is reasonably small.
43+ // Choose a threshold: allow suggestion only when the distance is reasonably small.
4444 // Here we allow suggestions when distance <= max(1, input.length()/3)
4545 int threshold = Math .max (1 , input .length () / 3 );
4646 if (bestDistance <= threshold ) {
@@ -62,24 +62,24 @@ private static int levenshteinDistance(String a, String b) {
6262 b = tmp ;
6363 }
6464
65- int [] prev = new int [a .length () + 1 ];
66- int [] curr = new int [a .length () + 1 ];
65+ int [] previous = new int [a .length () + 1 ];
66+ int [] current = new int [a .length () + 1 ];
6767
68- for (int i = 0 ; i <= a .length (); i ++) prev [i ] = i ;
68+ for (int i = 0 ; i <= a .length (); i ++) previous [i ] = i ;
6969
7070 for (int j = 1 ; j <= b .length (); j ++) {
71- curr [0 ] = j ;
71+ current [0 ] = j ;
7272 char bj = b .charAt (j - 1 );
7373 for (int i = 1 ; i <= a .length (); i ++) {
7474 int cost = (a .charAt (i - 1 ) == bj ) ? 0 : 1 ;
75- curr [i ] = min3 (curr [i - 1 ] + 1 , prev [i ] + 1 , prev [i - 1 ] + cost );
75+ current [i ] = min3 (current [i - 1 ] + 1 , previous [i ] + 1 , previous [i - 1 ] + cost );
7676 }
77- // swap prev and curr
78- int [] tmp = prev ;
79- prev = curr ;
80- curr = tmp ;
77+ // swap previous and current
78+ int [] temporary = previous ;
79+ previous = current ;
80+ current = temporary ;
8181 }
82- return prev [a .length ()];
82+ return previous [a .length ()];
8383 }
8484
8585 private static int min3 (int x , int y , int z ) {
0 commit comments