Skip to content

Instantly share code, notes, and snippets.

@stiteler
Last active September 13, 2017 21:18
Show Gist options
  • Save stiteler/f2dbf315c75d198aa5e9ab2fa1c51bc8 to your computer and use it in GitHub Desktop.
Save stiteler/f2dbf315c75d198aa5e9ab2fa1c51bc8 to your computer and use it in GitHub Desktop.
Longest common substring for many strings with @dokipen
from itertools import izip
def longest_common_substring(string_list):
def _iter():
for row in izip(*string_list):
s = set(row)
if len(s) == 1:
yield s.pop()
else:
return
return ''.join(_iter())
if __name__ == '__main__':
test = [
'abcdef',
'abcdefg',
'abcdefgh',
'abcdefghi'
'abcdefghij',
]
print longest_common_substring(test)
@stiteler
Copy link
Author

stiteler commented Apr 26, 2017

Original idea and code came from Eric here: http://stackoverflow.com/questions/18715688/find-common-substring-between-two-strings and @dokipen & I adapted it to accept many strings and to use izip

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment