src/main/java/eu/digitisation/distance/StringEditDistance.java
public static int levenshtein(String first, String second) {
        int i, j;
        int[][] A;

        // intialize
        A = new int[2][second.length() + 1];

        // Compute first row
        A[0][0] = 0;
        for (j = 1; j <= second.length(); ++j) {
            A[0][j] = A[0][j - 1] + 1;
        }

        // Compute other rows
        for (i = 1; i <= first.length(); ++i) {
            A[i % 2][0] = A[(i - 1) % 2][0] + 1;
            for (j = 1; j <= second.length(); ++j) {
                if (first.charAt(i - 1) == second.charAt(j - 1)) {
                    A[i % 2][j] = A[(i - 1) % 2][j - 1];
                } else {
                    A[i % 2][j] = min(A[(i - 1) % 2][j] + 1,
                            A[i % 2][j - 1] + 1,
                            A[(i - 1) % 2][j - 1] + 1);
                }
            }
        }
        return A[first.length() % 2][second.length()];
    }
/**
     * @param first the first string.
     * @param second the second string.
     * @return the Levenshtein distance between first and second.
     */
@param first the first string.
@param second the second string.
@return the Levenshtein distance between first and second.
@param first the first string . @param second the second string . @return the Levenshtein distance between first and second .
public static int levenshtein(String first, String second) {
        int i, j;
        int[][] A;
        A = new int[2][second.length() + 1];
        A[0][0] = 0;
        for (j = 1; j <= second.length(); ++j) {
            A[0][j] = A[0][j - 1] + 1;
        }
        for (i = 1; i <= first.length(); ++i) {
            A[i % 2][0] = A[(i - 1) % 2][0] + 1;
            for (j = 1; j <= second.length(); ++j) {
                if (first.charAt(i - 1) == second.charAt(j - 1)) {
                    A[i % 2][j] = A[(i - 1) % 2][j - 1];
                } else {
                    A[i % 2][j] = min(A[(i - 1) % 2][j] + 1,
                            A[i % 2][j - 1] + 1,
                            A[(i - 1) % 2][j - 1] + 1);
                }
            }
        }
        return A[first.length() % 2][second.length()];
    }
public static int levenshtein ( String first , String second ) { int i , j ; int [ ] [ ] A ; A = new int [ 2 ] [ second . length ( ) + 1 ] ; A [ 0 ] [ 0 ] = 0 ; for ( j = 1 ; j <= second . length ( ) ; ++ j ) { A [ 0 ] [ j ] = A [ 0 ] [ j - 1 ] + 1 ; } for ( i = 1 ; i <= first . length ( ) ; ++ i ) { A [ i % 2 ] [ 0 ] = A [ ( i - 1 ) % 2 ] [ 0 ] + 1 ; for ( j = 1 ; j <= second . length ( ) ; ++ j ) { if ( first . charAt ( i - 1 ) == second . charAt ( j - 1 ) ) { A [ i % 2 ] [ j ] = A [ ( i - 1 ) % 2 ] [ j - 1 ] ; } else { A [ i % 2 ] [ j ] = min ( A [ ( i - 1 ) % 2 ] [ j ] + 1 , A [ i % 2 ] [ j - 1 ] + 1 , A [ ( i - 1 ) % 2 ] [ j - 1 ] + 1 ) ; } } } return A [ first . length ( ) % 2 ] [ second . length ( ) ] ; }
@param first the first string.
@param first the first string .
// intialize // Compute first row // Compute other rows