![]() |
VOOZH | about |
Levenshtein distance is a measure of the similarity between two strings, which takes into account the number of insertion, deletion and substitution operations needed to transform one string into the other.
Let's see an example that there is String A: "kitten" which need to be converted in String B: "sitting" so we need to determine the minimum operation required
- kitten → sitten (substitution of “s” for “k”)
- sitten → sittin (substitution of “i” for ????”)
- sittin → sitting (insertion of “g” at the end).
In this case it took three operation do this, so the levenshtein distance will be 3.
The Levenshtein distance has various applications in various fields such as:
Let's see how Levenshtein distance is different from other distance metrics
To calculate the Levenshtein distance, In the recursive technique, we will use a simple recursive function. It checks each character in the two strings and performs recursive insertions, removals, and replacements.
Below is the implementation for the above idea:
Levenshtein Distance: 3
Time complexity: O(3^(m+n))
Auxiliary complexity: O(m+n)
The iterative technique with a full matrix uses a 2D matrix to hold the intermediate results of the Levenshtein distance calculation. It begins with empty strings and iteratively fills the matrix row by row. It computes the minimum cost of insertions, deletions, and replacements based on the characters of both strings.
Below is the implementation for the above idea:
Levenshtein Distance: 3
Time complexity: O(m*n)
Auxiliary complexity: O(m*n)
By simply storing two rows of the matrix at a time, the iterative technique with two matrix rows reduces space complexity. It iterates through the strings row by row, storing the current and past calculations in two rows.
Below is the implementation for the above approach:
Levenshtein Distance: 3
Time complexity: O(m*n)
Auxiliary Space: O(n)