Java Utililty Methods String Distance

List of utility methods to do String Distance

Description

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

Method

inteditDistance(CharSequence first, CharSequence second)
Returns the edit distance of the two CharSequences.
Uses Levenshtein algorithm.
int firstLength = first.length();
int secondLength = second.length();
int[][] distance = new int[firstLength + 1][secondLength + 1];
for (int f = 0; f <= firstLength; f++) {
    distance[f][0] = f;
for (int s = 0; s <= secondLength; s++) {
    distance[0][s] = s;
...
inteditDistance(String one, String two)
Determine the Levenshtein edit distance between two strings of characters
int n = one.length();
int m = two.length();
int[][] distance = new int[n + 1][m + 1];
for (int i = 0; i <= n; i++)
    distance[i][0] = i;
for (int j = 0; j <= m; j++)
    distance[0][j] = j;
for (int i = 1; i <= n; i++) {
...
inteditDistance(String s, String t)
The edit distance of the specified Strings (see Levenshtein distance).
int d[][]; 
int n; 
int m; 
int i; 
int j; 
char s_i; 
char t_j; 
int cost; 
...
inteditDistance(String s, String t)
Calculates the edit distance (aka Levenshtein distance) for two strings, i.e.
char[] sChars = s.toCharArray();
char[] tChars = t.toCharArray();
int m = s.length();
int n = t.length();
int[] distance = new int[m + 1];
for (int i = 0; i <= m; ++i) {
    distance[i] = i;
int[] oldDistance = new int[m + 1];
for (int j = 1; j <= n; ++j) {
    int[] tmp = oldDistance;
    oldDistance = distance;
    distance = tmp;
    distance[0] = j;
    for (int i = 1; i <= m; ++i) {
        int cost = 1 + Math.min(distance[i - 1], oldDistance[i]);
        if (sChars[i - 1] == tChars[j - 1]) {
            cost = Math.min(cost, oldDistance[i - 1]);
        } else {
            cost = Math.min(cost, 1 + oldDistance[i - 1]);
        distance[i] = cost;
return distance[m];
inteditDistance(String s, String t)
edit Distance
int m = s.length();
int n = t.length();
int[][] d = new int[m + 1][n + 1];
for (int i = 0; i <= m; i++) {
    d[i][0] = i;
for (int j = 0; j <= n; j++) {
    d[0][j] = j;
...
inteditDistance(String s0, String s1)
edit Distance
int len0 = s0.length() + 1;
int len1 = s1.length() + 1;
int[] cost = new int[len0];
int[] newcost = new int[len0];
for (int i = 0; i < len0; i++) {
    cost[i] = i;
for (int j = 1; j < len1; j++) {
...
intEditDistance(String s1, String s2)
Levenshtein Distance Taken from an implementation during an interview :p
if (s1.length() == 0) {
    return s2.length();
if (s2.length() == 0) {
    return s1.length();
int edit = (s1.charAt(0) == s2.charAt(0)) ? 0 : 1;
int add = EditDistance(s1.substring(1), s2) + 1;
...
doubleeditDistance(String str1, String str2)
edit Distance
int m = str1.length();
int n = str2.length();
str1 = str1.toUpperCase();
str2 = str2.toUpperCase();
int mat[][] = new int[m + 1][n + 1];
if (m == 0 || n == 0) {
    return Math.max(m, n);
} else {
...
inteditDistance(String string1, String string2)
edit Distance
int rowCount = string1.length() + 1;
int columnCount = string2.length() + 1;
int[][] matrix = new int[rowCount][columnCount];
for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) {
    matrix[rowIndex][0] = rowIndex;
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
    matrix[0][columnIndex] = columnIndex;
...
inteditDistance(String word1, String word2)
edit Distance
int len1 = word1.length();
int len2 = word2.length();
int[][] dp = new int[len1 + 1][len2 + 1];
for (int i = 0; i <= len1; i++) {
    dp[i][0] = i;
for (int j = 0; j <= len2; j++) {
    dp[0][j] = j;
...