The Levenshtein distance (a.k.a edit distance) is a measure of similarity between two strings. It is defined as the minimum number of changes required to convert string a
into string b
(this is done by inserting, deleting or replacing a character in string a
). The smaller the Levenshtein distance, the more similar the strings are. This is a very common problem in the application of Dynamic Programming.
A pre-requisite for applying Dynamic Programming, to solve a problem, is to demonstrate that the solution to the original problem can be found by using the solutions to the sub-problems.
The approach here is somewhat simple and intuitive. Consider the strings a
and b
up to their last character:
a
.Since DP utilises memory for storing the solutions to sub-problems, we will use a 2D array (D
) for this purpose.
The value in D[i][j]
represents the edit distance if we consider the strings a
and b
, till their i
th and j
th character, respectively. Therefore, the answer to the original problem will be found in D[length of 'a'][length of 'b']
.