Created
March 29, 2018 19:55
-
-
Save jianminchen/c0a5e8d944ed79051d9b5c1951da2906 to your computer and use it in GitHub Desktop.
deletion distance - being interviewer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
input: two strings | |
output: deletion distance | |
""" | |
def deletion_distance_helper(str1, str2, index1, index2): | |
if str1[index1] == str2[index2]: | |
deletion_distance_helper(str1, str2, index1 + 1, index2 + 1) | |
# base case | |
def deletion_distance(str1, str2): | |
if str1 == str2: | |
return 0 | |
using "dog" "frog" | |
"" "d" "o" "g" | |
"" "d" "do" "dog" | |
--------------------------------- | |
"" 0 1 2 3 | |
"f" "f" 1 2 3 4 | |
"r" "fr" 2 3 4 5 | |
"o" "fro" 3 4 3 | |
"g" "frog" 4 ? | |
dist("f", "d") = min( 1 + dist("", "d"), // delete "d" | |
1 + dist("f", "")) | |
dist("f","d") look up table -> left side entry, line 30 , first column | |
dist("d", "") look up talbe -> upper , line 29, second column | |
"fro", "do" = min(dist("fr", "d"), dist("fro", "d") + 1, dist("frog","d") + 1) | |
= dist("fr","d") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment