This is a well known problem.
Illustrating this problem using wikipedia's example
Making a table like Interleaving String
+---+---+---+---+---+---+---+---+
| | * | k | i | t | t | e | n |
+---+---+---+---+---+---+---+---+
| * | | | | | | | |
+---+---+---+---+---+---+---+---+
| s | | | | | | | |
+---+---+---+---+---+---+---+---+
| i | | | | | | | |
+---+---+---+---+---+---+---+---+
| t | | | | | | | |
+---+---+---+---+---+---+---+---+
| t | | | | | | | |
+---+---+---+---+---+---+---+---+
| i | | | | | | | |
+---+---+---+---+---+---+---+---+
| n | | | | | | | |
+---+---+---+---+---+---+---+---+
| g | | | | | | | |
+---+---+---+---+---+---+---+---+
* here is representing an empty string
each gird is the edit distance of string[0..x] and string[0..y]
First row and column girds are easy to be filled:
edit distanceof an empty string to an empty string is0edit distanceof an empty string to any string is the length of that string
So the table should be filled like:
+---+---+---+---+---+---+---+---+
| | * | k | i | t | t | e | n |
+---+---+---+---+---+---+---+---+
| * | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
+---+---+---+---+---+---+---+---+
| s | 1 | | | | | | |
+---+---+---+---+---+---+---+---+
| i | 2 | | | | | | |
+---+---+---+---+---+---+---+---+
| t | 3 | | | | | | |
+---+---+---+---+---+---+---+---+
| t | 4 | | | | | | |
+---+---+---+---+---+---+---+---+
| i | 5 | | | | | | |
+---+---+---+---+---+---+---+---+
| n | 6 | | | | | | |
+---+---+---+---+---+---+---+---+
| g | 7 | | | | | | |
+---+---+---+---+---+---+---+---+
in k's view
- Insert: cant transform to
s - Delete: cant transform to
s - Replace: replace
ktosuse1edit distance
in s's view
- Insert: cant transform to
k - Delete: cant transform to
k - Replace: replace
stokuse1edit distance
so the number in the gird k and s should be 1
- Insert: cant transform to
s - Delete: delete
ithen, transform to the problemsandkwe solved before. 1 +edit distanceofsandk. - Replace and Delete: replace
ktosuse1edit distance, then deletekuse1edit distance.1 + 1 = 2
Delete and Replace then Delete are the same thing at last, Replace is just the 1 edit distance costed by edit distance of s and k.
In this case, we can transform the problem by deleting one char: 1 + edit distance of string deleted one and k
We can fill up the second column and row now:
+---+---+---+---+---+---+---+---+
| | * | k | i | t | t | e | n |
+---+---+---+---+---+---+---+---+
| * | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
+---+---+---+---+---+---+---+---+
| s | 1 | 1 | 2 | 3 | 4 | 5 | 6 |
+---+---+---+---+---+---+---+---+
| i | 2 | 2 | | | | | |
+---+---+---+---+---+---+---+---+
| t | 3 | 3 | | | | | |
+---+---+---+---+---+---+---+---+
| t | 4 | 4 | | | | | |
+---+---+---+---+---+---+---+---+
| i | 5 | 5 | | | | | |
+---+---+---+---+---+---+---+---+
| n | 6 | 6 | | | | | |
+---+---+---+---+---+---+---+---+
| g | 7 | 7 | | | | | |
+---+---+---+---+---+---+---+---+
Obviously, si and ki equals to the edit distance of s and k, because i are both in two strings.
+---+---+---+---+---+---+---+---+
| | * | k | i | t | t | e | n |
+---+---+---+---+---+---+---+---+
| * | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
+---+---+---+---+---+---+---+---+
| s | 1 | 1 | 2 | 3 | 4 | 5 | 6 |
+---+---+---+---+---+---+---+---+
| i | 2 | 2 | 1 | | | | |
+---+---+---+---+---+---+---+---+
| t | 3 | 3 | | | | | |
+---+---+---+---+---+---+---+---+
| t | 4 | 4 | | | | | |
+---+---+---+---+---+---+---+---+
| i | 5 | 5 | | | | | |
+---+---+---+---+---+---+---+---+
| n | 6 | 6 | | | | | |
+---+---+---+---+---+---+---+---+
| g | 7 | 7 | | | | | |
+---+---+---+---+---+---+---+---+
then we can fill up the table using technology we used before.
P[word1][word2] is the the table,
so P[i][j] have 4 choices:
P[i - 1][j] + 1: by deletingword1[i]fromword1and convert to previous problem.P[i][j - 1] + 1: by deletingword2[j]fromword2and convert to previous problem.P[i - 1][j - 1]: only ifword1[i] == word2[j]P[i - 1][j - 1] + 1: only ifword1[i] != word2[j], then replace one charword1[i]toword2[j], then this problem is converted toP[i - 1][j - 1].
Dont worry about Insertion, it is just the oppsite to Deletion. Just different view.
So the work remaing is easy, find the MIN of those four is ok.