How to find the Levenshtein distance between two strings of characters, in Java?

How to find the Levenshtein distance between two strings of characters, in Java?

The Levenshtein distance is a measure of the similarity between two strings of characters. It is defined as the minimum number of single-character edits (insertions, deletions, or substitutions) required to transform one string into the other.

To find the Levenshtein distance between two strings in Java, you can use a dynamic programming algorithm.

Here's an example of how to find the Levenshtein distance between two strings in Java using a dynamic programming approach:

ref‮ot re‬:lautturi.com
public static int getLevenshteinDistance(String s1, String s2) {
  int m = s1.length();
  int n = s2.length();
  int[][] dp = new int[m + 1][n + 1];

  for (int i = 0; i <= m; i++) {
    for (int j = 0; j <= n; j++) {
      if (i == 0) {
        dp[i][j] = j;
      } else if (j == 0) {
        dp[i][j] = i;
      } else if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
        dp[i][j] = dp[i - 1][j - 1];
      } else {
        dp[i][j] = Math.min(Math.min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1;
      }
    }
  }

  return dp[m][n];
}

In the above example, a method called getLevenshteinDistance() is defined which takes two String objects as arguments and returns an integer.

The method first declares two variables m and n which hold the lengths of the two strings. It then declares a 2D array called dp with m + 1 rows and n + 1 columns.

A nested loop is then used to fill the array with the minimum number of single-character edits required to transform one string into the other. The outer loop iterates over the rows and the inner loop iterates over the columns.

For each element in the array, the following conditions are checked:

  • If either i or j is 0, the element is set to the value of i or j, respectively.
  • If the characters at positions i - 1 and j - 1 in the two strings are equal, the element is set to the value of the element in the previous row and column.
  • If the characters at positions i - 1 and j - 1 in the two strings are not equal, the element is set to the minimum of the elements in the previous row, previous column, and previous row and column, plus 1.

After the loops complete, the value of the element in the last row and column is returned as the Levenshtein distance between the two strings.

You can use the getLevenshteinDistance() method like this:

String s1 = "kitten";
String s2 = "sitting";
int distance = getLevenshteinDistance(s1, s2);
System.out.println("The Levenshtein distance between '" + s1 + "' and '" + s2 + "' is " + distance);
Created Time:2017-11-01 20:42:47  Author:lautturi