Levenshtein distance between two strings is defined as the minimum number of characters needed to insert, delete or replace in a given string string1 to transform it to another string string2.
Input : string1 = “geek”, string2 = “gesek”
Output : 1
Explanation : We can convert string1 into str2 by inserting a ‘s’.
Input : str1 = “cat”, string2 = “cut”
Output : 1
Explanation : We can convert string1 into str2 by replacing ‘a’ with ‘u’.
Input : string1 = “sunday”, string2 = “saturday”
Output : 3
Explanation : Last three and first characters are same. We basically need to convert “un” to “atur”. This can be done using below three operations. Replace ‘n’ with ‘r’, insert t, insert a
The Levenshtein distance between two strings can be found using the
enchant.utils.levenshtein() method of the
Syntax : enchant.utils.levenshtein(string1, string2)
string1 : the first string to be compared
string2 : the second string to be compared
Returns : an integer denoting the Levenshtein distance
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.