Advertisement
Guest User

Untitled

a guest
Apr 5th, 2011
960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. string1 = "abcdefghijklmnopqrstuvwxyz"
  2. string2 = "111ijklmno1111klmnopqrstuvwxyz1111111wxy111111"
  3. #Make sure string 1 is the shorter string, to be more efficient
  4. if len(string1)>len(string2):
  5.     string1, string2 = string2, string1
  6. substr = ""
  7. #Loop through each possible start for a substring
  8. for start in range(len(string1)):
  9.     #Loop through each possible ending for the current start
  10.     for end in range(start,len(string1)):
  11.         sub = string1[start:end+1]
  12.         #if it's in the other string, it's common
  13.         if sub in string2:
  14.             #if it's longer than the current longest common substring found
  15.             if len(sub)>len(substr):
  16.                 #it's the new longest common substring
  17.                 substr = sub
  18.         #otherwise, this start is wrong, move on to the next start
  19.         else:
  20.             break
  21. print substr
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement