Levenshtein Distance in Java - Fuzzy Logic to match names or any strings

Levenshtein Distance is a well-known algorithm that takes two strings and returns an integer representing the number of changes that need to be made to one string in order to have it transformed to the second string.

Apache Commons Lang library already has a method in the StringUtils class for this called getLevenshteinDistance. That’s nice to know so that you don’t have to implement your own. You can use the Levenshtein Distance to perform “fuzzy matching” between two strings with a calculation like:

percentMatch = 1 – (levenshteinDistance / longestStringLength) * 100

The “longestStringLength” is the length of the longer of the two strings (use something like Math.max(a.length(), b.length())).