Levenshtein Distance editing distance

Source: Internet
Author: User

The editing distance, also known as the Levenshtein distance, is the minimum number of edit operations required between two strings, from one to another. Permission edits include replacing one character with another, inserting a character, deleting a character

Implementation scenarios:

1. Find out the longest common substring length

Reference code:

Apache Commons-lang

public static int Getlevenshteindistance (charsequence s, charsequence t) {
if (s = = NULL | | t = = NULL) {
throw new IllegalArgumentException ("Strings must not is null");
}

/*
The difference between this impl. And the previous is that, rather
than creating and retaining a matrix of size s.length () + 1 by t.length () + 1,
We maintain single-dimensional arrays of length s.length () + 1. The first, D,
Is the ' current working ' distance array, that maintains, the newest distance cost
Counts as we iterate through the characters of String S. Each time we increment
The index of the String t we are comparing, D is copied to P, the second int[]. Doing so
Allows us to retain the previous cost counts as required by the algorithm (taking
The minimum of the cost count to the left, up one, and diagonally
of the current cost count being calculated). (Note that the arrays aren ' t really
Copied anymore, just Switched...this is clearly much better than cloning an array
or doing a system.arraycopy () each time through the outer loop.)

Effectively, the difference between the "the" and "implementations is" this one does not
Cause an out of memory condition when calculating, the LD over the very large strings.
*/

int n = s.length (); Length of S
int m = T.length (); Length of T

if (n = = 0) {
return m;
} else if (M = = 0) {
return n;
}

if (n > m) {
Swap the input strings to consume less memory
Final charsequence tmp = s;
s = t;
t = tmp;
n = m;
m = T.length ();
}

int p[] = new Int[n + 1]; ' Previous ' cost array, horizontally
int d[] = new Int[n + 1]; Cost Array, horizontally
int _d[]; Placeholder to assist in swapping P and D

Indexes into strings s and T
int i; Iterates through S
Int J; Iterates through T

Char T_j; jth character of T

int cost; Cost

for (i = 0; I <= N; i++) {
P[i] = i;
}

for (j = 1; j <= M; j + +) {
T_j = T.charat (j-1);
D[0] = j;

for (i = 1; I <= n; i++) {
Cost = S.charat (i-1) = = T_j? 0:1;
Minimum of cell to the left+1, to the top+1, diagonally left and up +cost
D[i] = Math.min (Math.min (d[i-1] + 1, p[i] + 1), p[i-1] + cost);
}

Copy current distance counts to ' previous row ' distance counts
_d = p;
p = D;
D = _d;
}

Our last action in the above loop is to switch D and P, so p now
Actually have the most recent cost counts
return p[n];
}

Levenshtein Distance editing distance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.