static int levenIC(S s, S t) { // degenerate cases if (eqic(s, t)) ret 0; int ls = l(s), lt = l(t); if (ls == 0) ret lt; if (lt == 0) ret ls; // create two work vectors of integer distances int[] v0 = new int[lt + 1]; int[] v1 = new int[lt + 1]; // initialize v0 (the previous row of distances) // this row is A[0][i]: edit distance for an empty s // the distance is just the number of characters to delete from t for (int i = 0; i < v0.length; i++) v0[i] = i; for (int i = 0; i < ls; i++) { // calculate v1 (current row distances) from the previous row v0 // first element of v1 is A[i+1][0] // edit distance is delete (i+1) chars from s to match empty t v1[0] = i + 1; // use formula to fill in the rest of the row for (int j = 0; j < lt; j++) { int cost = equalsIgnoreCase(s.charAt(i), t.charAt(j)) ? 0 : 1; v1[j + 1] = Math.min(Math.min( v1[j] + 1, // delete v0[j + 1] + 1), // insert v0[j] + cost); // edit } // swap arrays int[] v = v1; v1 = v0; v0 = v; } return v0[lt]; }