LANG-1234: getLevenshteinDistance with a threshold: optimize implementation if the strings lengths differ more than the threshold (closes #118)

If the string lengths differ more than the threshold, there's no need for the algorithm to begin allocating arrays etc.
This commit is contained in:
Jonatan Jönsson 2015-11-26 10:23:15 +01:00 committed by pascalschumacher
parent 031f6b082f
commit 04b41e2c97
1 changed files with 4 additions and 0 deletions

View File

@ -7585,6 +7585,10 @@ distance is O(nm), but a bound of k allows us to reduce it to O(km) time by only
} else if (m == 0) {
return n <= threshold ? n : -1;
}
// no need to calculate the distance if the length difference is greater than the threshold
else if (Math.abs(n - m) > threshold) {
return -1;
}
if (n > m) {
// swap the two strings to consume less memory