Skip to content

Instantly share code, notes, and snippets.

@stevekrenzel
Created February 27, 2011 06:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevekrenzel/845969 to your computer and use it in GitHub Desktop.
Save stevekrenzel/845969 to your computer and use it in GitHub Desktop.
Find the longest common substring between two strings.
from collections import defaultdict
def longest_common_substring(a, b):
index = defaultdict(list)
for i, c in enumerate(b):
index[c].append(i)
mxi, mxj, mxsz = 0, 0, 0
matches = defaultdict(int)
for i, c in enumerate(a):
new_matches = defaultdict(int)
for j in index[c]:
k = new_matches[j] = matches[j - 1] + 1
if k > mxsz:
mxi, mxj, mxsz = i, j, k
matches = new_matches
return mxi - mxsz + 1, mxj - mxsz + 1, mxsz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment