VOOZH about

URL: https://www.geeksforgeeks.org/dsa/introduction-to-levenshtein-distance/

⇱ Introduction to Levenshtein distance - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Introduction to Levenshtein distance

Last Updated : 31 Jan, 2024

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. 

Operations in Levenshtein distance are:

  • Insertion: Adding a character to string A.
  • Deletion: Removing a character from string A.
  • Replacement: Replacing a character in string A with another character.

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.

  • Upper and lower bounds: If and only if the two strings are identical, the Levenshtein distance is always non-negative and zero. Because it requires completely changing one string into the other through deletions or insertions, the most feasible Levenshtein distance between two strings of length m and n is max(m, n).

Applications of Levenshtein distance:

The Levenshtein distance has various applications in various fields such as:

  • Autocorrect Algorithms: Text editors and messaging applications use the Levenshtein distance in their autocorrect features such as gboard, swift keyboard, etc.
  • Data cleaning: It is widely used in the process of data cleaning and normalization task to reduce redundancy and identify similar records in the data mining process.
  • Data clustering and classification: To identify similar records and cluster them is clustering while identifying similar records and providing them with class labels is classification

Relationship with other edit distance metrics:

Let's see how Levenshtein distance is different from other distance metrics

  • Damerau-Levenshtein distance: It is similar to the Levenshtein distance, but it just also allows transpositions as an additional operation making it 4 operations.
  • Hamming distance: It can only be applied to strings of equal length, it is used measures the number of positions at which the corresponding characters are different.

Now let's see its implementation using different approaches in different approaches:

1) Levenshtein distance using a recursive approach

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:


Output
Levenshtein Distance: 3

Time complexity: O(3^(m+n))
Auxiliary complexity: O(m+n)

2) Levenshtein distance using Iterative with the full matrix approach

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:


Output
Levenshtein Distance: 3

Time complexity: O(m*n)
Auxiliary complexity: O(m*n)

3) Levenshtein distance using Iterative with two matrix rows approach

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:


Output
Levenshtein Distance: 3

Time complexity: O(m*n)
Auxiliary Space: O(n)

Comment
Article Tags:
Article Tags: