Created
June 1, 2014 03:51
-
-
Save hillscottc/947a1f5ddd01bdc85c72 to your computer and use it in GitHub Desktop.
Longest Common Substring, Dynamic
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
"""http://stackoverflow.com/questions/2892931/longest-common-substring-from-more-than-two-strings-python""" | |
def long_substr(data): | |
"""Finds the longest common string in any arbitrary array of strings""" | |
substr = '' | |
if len(data) > 1 and len(data[0]) > 0: | |
for i in range(len(data[0])): | |
for j in range(len(data[0])-i+1): | |
if j > len(substr) and all(data[0][i:i+j] in x for x in data): | |
substr = data[0][i:i+j] | |
return substr | |
print long_substr(['Oh, hello, my friend.', | |
'I prefer Jelly Belly beans.', | |
'When hell freezes over!']) |
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
"""From wikipedia. Not as clean as could be. | |
You don't really need to create the matrix explicitly. | |
Only handles two strings. | |
But, it's a good port of how a more typical language would do it.""" | |
def longest_common_substring(s1, s2): | |
"""Finds the longest common string given two strings""" | |
m = [[0] * (1 + len(s2)) for i in xrange(1 + len(s1))] | |
longest, x_longest = 0, 0 | |
for x in xrange(1, 1 + len(s1)): | |
for y in xrange(1, 1 + len(s2)): | |
if s1[x - 1] == s2[y - 1]: | |
m[x][y] = m[x - 1][y - 1] + 1 | |
if m[x][y] > longest: | |
longest = m[x][y] | |
x_longest = x | |
else: | |
m[x][y] = 0 | |
return s1[x_longest - longest: x_longest] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment