Created
March 14, 2017 17:56
-
-
Save fish2000/ea7e0822a5b45fc1a64ac50c16ec7b11 to your computer and use it in GitHub Desktop.
Almost ASCII Deletion Distance
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
def ascii_deletion_distance(str1, str2): | |
from collections import defaultdict | |
histogram = defaultdict(int) | |
for ch in str1: | |
histogram[ch] += 1 | |
for ch in str2: | |
histogram[ch] += 1 | |
union = set(str1) | set(str2) | |
intersection = set(str1) & set(str2) | |
result = union - intersection | |
# values = [ord(ch) for ch in result] | |
out = 0 | |
for ch in result: | |
out += (histogram[ch] * ord(ch)) | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As we can see it doesn't hit all scenarios.
Here's my solution to the problem.
I know this is not the best solution, as it's possible to get this working in an O(n) case. But given the time I had to solve it... It was rushed.
The worst case is O(n^2), with a best case of O(n)
worst case
abcd
dcba
will take O(n^2)
best case
abcd
abcd
will take O(n)