0 <= i <= N and 0 <= j <= M
We fill this table in a bottom-up manner. This is essentially solving the problem by combining the results of it’s sub-problems. (Dynamic programming). The entries are filled using the following formula
table[0][i] = i
table[i][0] = i
Explanation:
Assume that 0th character is empty, the edit distance between an empty string and a string of length ‘i’ is always ‘i’ because by deleting all the ‘i’ characters, it becomes an empty string.
table[i][j] = table[i-1][j-1] if string1[i] = string2[j]
Explanation:
If string1[i] and string2[j] are equal, we don’t need any modifications. Hence it is as same as table[i-1][j-1]
table[i][j] = Min(table[i-1][j],table[i][j-1],table[i-1][j-1]) + 1
Explanation:
If string1[i] and string2[j] are not equal, we have three choices.
- Modify the differing character string1[i] to match string2[j]. This requires a cost of table[i-1][j-1] + 1
- Delete string1[i] and try to match the remaining with string2. This requires a cost of table[i-1][j] + 1
- Insert string1[i] into string2. This requires a cost of table[i][j-1] + 1.
Finally table[N][M] contains the required answer.
The time and space complexity for this algorithm is O(n * m).
#include <iostream> | |
using namespace std; | |
int table[101][101]; | |
int getEditDistance( string str1, string str2 ) | |
{ | |
int len1 = str1.length(); | |
int len2 = str2.length(); | |
int i, j; | |
//Base cases | |
for( i = 0; i <= len1; i++ ) | |
{ | |
table[i][0] = i; | |
} | |
for( i = 0; i <= len2; i++ ) | |
{ | |
table[0][i] = i; | |
} | |
for( i = 1; i <= len1; i++ ) | |
{ | |
for( j= 1; j <= len2; j++ ) | |
{ | |
if( str1[i-1] == str2[j-1] ) // no modifications required | |
table[i][j] = table[i-1][j-1]; | |
else | |
{ | |
int edits = table[i-1][j]; | |
edits = min( edits, table[i][j-1] ); | |
edits = min( edits, table[i-1][j-1]); | |
table[i][j] = edits + 1; | |
} | |
} | |
} | |
return table[len1][len2]; | |
} | |
int main() | |
{ | |
string str1, str2; | |
cin >> str1 >> str2; | |
cout << getEditDistance( str1, str2 ); | |
return 0; | |
} |