Advertisement
chaosagent

Longest Common Substring

Jun 25th, 2015
320
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. string1 = 'thisisatest'
  2. string2 = 'testing123testing'
  3.  
  4. results = [[None] * len(string2) for i in xrange(len(string1))]
  5.  
  6. def solve(string1, string2, i1, i2):
  7.     if i1 == len(string1) or i2 == len(string2): return ''
  8.     if results[i1][i2] is None:
  9.         if string1[i1] == string2[i2]:
  10.             results[i1][i2] = string1[i1] + solve(string1, string2, i1 + 1, i2 + 1)
  11.         else:
  12.             rright = solve(string1, string2, i1, i2 + 1)
  13.             rleft = solve(string1, string2, i1 + 1, i2)
  14.             results[i1][i2] = rleft if len(rleft) > len(rright) else rright
  15.     return results[i1][i2]
  16. print solve(string1, string2, 0, 0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement