Java Utililty Methods String Levenshtein Distance

List of utility methods to do String Levenshtein Distance

Description

The list of methods to do String Levenshtein Distance are organized into topic(s).

Method

intlevenshteinDistance(String s, String t)
Derived from pseudocode at: http://en.wikipedia.org/wiki/Levenshtein_distance Used to determine edit distances between two strings
int m = s.length();
int n = t.length();
int dtable[][] = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
    dtable[i][0] = i; 
for (int j = 0; j <= n; j++) {
    dtable[0][j] = j; 
...
intLevenshteinDistance(String s, String t)
Levenshtein Distance
int len_s = s.length();
int len_t = t.length();
int last_s = s.length() - 1;
int last_t = t.length() - 1;
int cost = 0;
if (len_s == 0)
    return len_t;
if (len_t == 0)
...
intlevenshteinDistance(String s, String t)
levenshtein Distance
int m = s.length();
int n = t.length();
int d[][] = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
        d[i][j] = 0;
for (int i = 1; i <= m; i++) {
    d[i][0] = i;
for (int j = 1; j <= n; j++) {
    d[0][j] = j;
for (int j = 1; j <= n; j++) {
    for (int i = 1; i <= m; i++) {
        if (s.charAt(i - 1) == t.charAt(j - 1)) {
            d[i][j] = d[i - 1][j - 1];
        } else {
            d[i][j] = getMin(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + 1);
return d[m][n];
intlevenshteinDistance(String s, String t)
Computes the Levensthein distance between two given strings.
int d[][]; 
int n; 
int m; 
int i; 
int j; 
char s_i; 
char t_j; 
int cost; 
...
intlevenshteinDistance(String s, String t)
levenshtein Distance
int d[][]; 
int n; 
int m; 
int i; 
int j; 
char s_i; 
char t_j; 
int cost; 
...
intlevenshteinDistance(String s, String t, int limit)
Rank two strings similarity in terms of distance The lower the number, the more similar these strings are to each other See: http://en.wikipedia.org/wiki/Levenshtein_distance#Computing_Levenshtein_distance
int sizeDiff = Math.abs(s.length() - t.length());
if (sizeDiff > limit) {
    return sizeDiff;
s = s.toLowerCase().replaceAll("[^a-zA-Z0-9\\s]", "");
t = t.toLowerCase().replaceAll("[^a-zA-Z0-9\\s]", "");
int m = s.length();
int n = t.length();
...
intlevenshteinDistance(String s1, String s2)
I shamefully stole this from: http://rosettacode.org/wiki/Levenshtein_distance#Java --Gabor
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
int[] costs = new int[s2.length() + 1];
for (int i = 0; i <= s1.length(); i++) {
    int lastValue = i;
    for (int j = 0; j <= s2.length(); j++) {
        if (i == 0)
            costs[j] = j;
...
intlevenshteinDistance(String s1, String s2)
I shamefully stole this from: http://rosettacode.org/wiki/Levenshtein_distance#Java --Gabor
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
int[] costs = new int[s2.length() + 1];
for (int i = 0; i <= s1.length(); i++) {
    int lastValue = i;
    for (int j = 0; j <= s2.length(); j++) {
        if (i == 0)
            costs[j] = j;
...
intlevenshteinDistance(String st1, String st2)
Levensthein Distance between two Strings
int n = st1.length(), m = st2.length();
if (n == 0)
    return m;
if (m == 0)
    return n;
int[][] distance = new int[n + 1][m + 1];
for (int i = 0; i <= n; i++)
    distance[i][0] = i;
...
intlevenshteinDistance(String string1, String string2)
levenshtein Distance
char[] s1 = string1.toCharArray();
char[] s2 = string2.toCharArray();
int n1 = s1.length;
int n2 = s2.length;
int[][] d = new int[n1 + 1][n2 + 1];
for (int i = 1; i < n1 + 1; i++) {
    d[i][0] = i;
for (int j = 1; j < n2 + 1; j++) {
    d[0][j] = j;
for (int j = 0; j < n2; j++) {
    for (int i = 0; i < n1; i++) {
        int substitutionCost;
        if (s1[i] == s2[j]) {
            substitutionCost = 0;
        } else {
            substitutionCost = 1;
        d[i + 1][j + 1] = Math.min(Math.min(d[i][j + 1] + 1, d[i + 1][j] + 1), d[i][j] + substitutionCost);
return d[n1][n2];