@@ -203,15 +203,15 @@ public static final boolean isPalindromeInPlace(String string) {
203203 return true ;
204204 }
205205
206- public static final String [] generateSubsets (String input ) {
207- int length = input .length ();
208- int size = (int ) Math .pow (2 , length );
209- BitSet [] sets = new BitSet [size ];
210- String [] output = new String [size ];
206+ public static final String [] generateSubsets (String inputString ) {
207+ final int length = inputString .length ();
208+ final int size = (int ) Math .pow (2 , length );
209+ final BitSet [] sets = new BitSet [size ];
210+ final String [] output = new String [size ];
211211
212212 for (int i = 0 ; i < size ; i ++) {
213- BitSet set = new BitSet (size );
214- StringBuilder builder = new StringBuilder ();
213+ final BitSet set = new BitSet (size );
214+ final StringBuilder builder = new StringBuilder ();
215215 if (i > 0 ) {
216216 for (int j = length - 1 ; j >= 0 ; j --) {
217217 if (j == length - 1 ) {
@@ -228,13 +228,12 @@ public static final String[] generateSubsets(String input) {
228228 set .set (j , prev );
229229 }
230230 if (set .get (j ))
231- builder .append (input .charAt (j ));
231+ builder .append (inputString .charAt (j ));
232232 }
233233 }
234234 sets [i ] = set ;
235235 output [i ] = builder .toString ();
236236 }
237-
238237 return output ;
239238 }
240239
@@ -268,21 +267,21 @@ private static final int permutations(String[] list, int index, char[] prefix, c
268267 }
269268
270269 /** N! permutation of the characters of the string (in order) **/
271- public static String [] permutations (String string ) {
272- final int size = numberOfPermutations (string .length ());
270+ public static String [] permutations (String stringToGeneratePermutationsFrom ) {
271+ final int size = numberOfPermutations (stringToGeneratePermutationsFrom .length ());
273272 final String [] list = new String [size ];
274273 final char [] prefix = new char [0 ];
275- final char [] chars = string .toCharArray ();
274+ final char [] chars = stringToGeneratePermutationsFrom .toCharArray ();
276275 permutations (list , 0 , prefix , chars , 0 , chars .length );
277276 return list ;
278277 }
279278
280- public static final int levenshteinDistance ( String s , String t ) {
281- int sLength = s . length ();
282- int tLength = t .length ();
283-
284- char [] sChars = s .toCharArray ();
285- char [] tChars = t .toCharArray ();
279+ /** recursive **/
280+ public static final int levenshteinDistanceRecursive ( String s , String t ) {
281+ final int sLength = s .length ();
282+ final int tLength = t . length ();
283+ final char [] sChars = s .toCharArray ();
284+ final char [] tChars = t .toCharArray ();
286285
287286 int cost = 0 ;
288287 if ((sLength > 0 && tLength > 0 ) && sChars [0 ] != tChars [0 ])
@@ -293,12 +292,51 @@ public static final int levenshteinDistance(String s, String t) {
293292 else if (tLength == 0 )
294293 return sLength ;
295294 else {
296- int min1 = levenshteinDistance (s .substring (1 ), t ) + 1 ;
297- int min2 = levenshteinDistance (s , t .substring (1 )) + 1 ;
298- int min3 = levenshteinDistance (s .substring (1 ), t .substring (1 )) + cost ;
295+ final int min1 = levenshteinDistanceRecursive (s .substring (1 ), t ) + 1 ;
296+ final int min2 = levenshteinDistanceRecursive (s , t .substring (1 )) + 1 ;
297+ final int min3 = levenshteinDistanceRecursive (s .substring (1 ), t .substring (1 )) + cost ;
299298
300299 int minOfFirstSet = Math .min (min1 , min2 );
301300 return (Math .min (minOfFirstSet , min3 ));
302301 }
303302 }
303+
304+ /** iterative - dynamic programming **/
305+ public static final int levenshteinDistanceIterative (String string1 , String string2 ) {
306+ final char [] s = string1 .toCharArray ();
307+ final char [] t = string2 .toCharArray ();
308+ final int m = s .length ;
309+ final int n = t .length ;
310+
311+ // for all i and j, d[i,j] will hold the Levenshtein distance between
312+ // the first i characters of s and the first j characters of t
313+ // note that d has (m+1)*(n+1) values
314+ final int [][] d = new int [m +1 ][n +1 ];
315+
316+ // source prefixes can be transformed into empty string by
317+ // dropping all characters
318+ for (int i =1 ; i <=m ; i ++)
319+ d [i ][0 ] = i ;
320+
321+ // target prefixes can be reached from empty source prefix
322+ // by inserting every character
323+ for (int j =1 ; j <=n ; j ++)
324+ d [0 ][j ] = j ;
325+
326+ int substitutionCost ;
327+ for (int j =1 ; j <=n ; j ++) {
328+ for (int i =1 ; i <=m ; i ++) {
329+ if (s [i -1 ] == t [j -1 ])
330+ substitutionCost = 0 ;
331+ else
332+ substitutionCost = 1 ;
333+
334+ int minOfInsAndDel = Math .min (d [i -1 ][j ] + 1 , // deletion
335+ d [i ][j -1 ] + 1 ); // insertion
336+ d [i ][j ] = Math .min (minOfInsAndDel , // minimum of insert and delete
337+ d [i -1 ][j -1 ] + substitutionCost ); // substitution
338+ }
339+ }
340+ return d [m ][n ];
341+ }
304342}
0 commit comments