Created
November 12, 2012 10:19
-
-
Save fritz0705/4058512 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# This is O(scary) | |
def match_string(hay, nee): | |
if not nee: | |
return 0 | |
index = {} | |
for offset, c in zip(range(0, len(hay)), hay): | |
if c not in index: | |
index[c] = [] | |
index[c].append(offset) | |
indices = [] | |
try: | |
for c in nee: | |
indices.append(index[c]) | |
except KeyError: | |
return None | |
pivot = 0 | |
new_indices = [] | |
for i in indices: | |
i = list(filter(lambda x: x >= pivot, i)) | |
pivot = i[0] | |
new_indices.append(i) | |
indices = new_indices | |
del new_indices | |
combinations = len(indices[0]) | |
start_off = 0 | |
n = 0 | |
while n < combinations: | |
new_indices = [] | |
i_iter = iter(indices) | |
pivot = next(i_iter)[start_off] | |
new_indices.append(pivot) | |
fail = False | |
for i in indices[1:]: | |
f = list(filter(lambda x: x > pivot, i)) | |
if not f: | |
fail = True | |
break | |
if pivot + 1 != f[0]: | |
fail = True | |
break | |
pivot = f[0] | |
new_indices.append(pivot) | |
if fail: | |
start_off += 1 | |
n += 1 | |
continue | |
indices = new_indices | |
break | |
return indices[0] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment