Created
August 9, 2017 23:24
-
-
Save kennycason/001f37d2f7093ee9cf0eb57160a626d1 to your computer and use it in GitHub Desktop.
Longest Repeated SubString
This file contains hidden or 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
def lrss(s): | |
slen = len(s) | |
for l in range(slen - 1, 1, -1): # the length of the string, go from max to min, decrementing range | |
print "l=" + str(l) | |
# there is a bit of redundancy in the i/j iterations as they double check each other | |
# e.g. when i < j and j > i | |
for i in range(0, slen - l + 1): # tracks position of the iterations | |
print " i=" + str(i) | |
for j in range(0, slen - l + 1): # tracks position of the iterations | |
if i == j: continue # if i == j , then it's the same index in the string | |
print " j=" + str(j) | |
for k in range(0, l): | |
print " k=" + str(k) | |
if s[i + k] != s[j + k]: # stop checking! | |
break | |
if k == l - 1: # at this point we know they are equal, | |
# so lets determine if this is the last character to check | |
print "found match at i=" + str(i) + ", j=" + str(j) + " of length=" + str(l) | |
return s[i : i + l] # return substring, s[from index, to index] | |
return None | |
print lrss('banana') # ana | |
print lrss('bananaisbanana') # banana | |
print lrss('abcdefg') # None | |
# the l,i,j iterations seemed sane after i added + 1 to the i/j ranges | |
# prior to adding the + 1 to ranges i noticed it was not checking all the i/j cases i expected | |
# l=5 | |
# i=0 | |
# j=1 | |
# i=1 | |
# j=0 | |
# l=4 | |
# i=0 | |
# j=1 | |
# j=2 | |
# i=1 | |
# j=0 | |
# j=2 | |
# i=2 | |
# j=0 | |
# j=1 | |
# l=3 | |
# i=0 | |
# j=1 | |
# j=2 | |
# j=3 | |
# i=1 | |
# j=0 | |
# j=2 | |
# j=3 | |
# i=2 | |
# j=0 | |
# j=1 | |
# j=3 | |
# i=3 | |
# j=0 | |
# j=1 | |
# j=2 | |
# l=2 | |
# i=0 | |
# j=1 | |
# j=2 | |
# j=3 | |
# j=4 | |
# i=1 | |
# j=0 | |
# j=2 | |
# j=3 | |
# j=4 | |
# i=2 | |
# j=0 | |
# j=1 | |
# j=3 | |
# j=4 | |
# i=3 | |
# j=0 | |
# j=1 | |
# j=2 | |
# j=4 | |
# i=4 | |
# j=0 | |
# j=1 | |
# j=2 | |
# j=3 | |
# after adding the k logging i noticed that the k's are not being iterated as much as i'd expect | |
# also we could stop checking earlier once s[i + k] != s[j + k], so i'll rewrite that | |
# current output with ks without fixing the off-by-one error | |
# l=5 | |
# i=0 | |
# j=1 | |
# k=0 | |
# k=1 | |
# k=2 | |
# k=3 | |
# i=1 | |
# j=0 | |
# k=0 | |
# k=1 | |
# k=2 | |
# k=3 | |
# l=4 | |
# i=0 | |
# j=1 | |
# k=0 | |
# k=1 | |
# k=2 | |
# j=2 | |
# k=0 | |
# k=1 | |
# k=2 | |
# i=1 | |
# j=0 | |
# k=0 | |
# k=1 | |
# k=2 | |
# j=2 | |
# k=0 | |
# k=1 | |
# k=2 | |
# i=2 | |
# j=0 | |
# k=0 | |
# k=1 | |
# k=2 | |
# j=1 | |
# k=0 | |
# k=1 | |
# k=2 | |
# l=3 | |
# i=0 | |
# j=1 | |
# k=0 | |
# k=1 | |
# j=2 | |
# k=0 | |
# k=1 | |
# j=3 | |
# k=0 | |
# k=1 | |
# i=1 | |
# j=0 | |
# k=0 | |
# k=1 | |
# j=2 | |
# k=0 | |
# k=1 | |
# j=3 | |
# k=0 | |
# k=1 | |
# i=2 | |
# j=0 | |
# k=0 | |
# k=1 | |
# j=1 | |
# k=0 | |
# k=1 | |
# j=3 | |
# k=0 | |
# k=1 | |
# i=3 | |
# j=0 | |
# k=0 | |
# k=1 | |
# j=1 | |
# k=0 | |
# k=1 | |
# j=2 | |
# k=0 | |
# k=1 | |
# l=2 | |
# i=0 | |
# j=1 | |
# k=0 | |
# j=2 | |
# k=0 | |
# j=3 | |
# k=0 | |
# j=4 | |
# k=0 | |
# i=1 | |
# j=0 | |
# k=0 | |
# j=2 | |
# k=0 | |
# j=3 | |
# k=0 | |
# j=4 | |
# k=0 | |
# i=2 | |
# j=0 | |
# k=0 | |
# j=1 | |
# k=0 | |
# j=3 | |
# k=0 | |
# j=4 | |
# k=0 | |
# i=3 | |
# j=0 | |
# k=0 | |
# j=1 | |
# k=0 | |
# j=2 | |
# k=0 | |
# j=4 | |
# k=0 | |
# i=4 | |
# j=0 | |
# k=0 | |
# j=1 | |
# k=0 | |
# j=2 | |
# k=0 | |
# j=3 | |
# k=0 | |
# after adding + 1 to k range it output | |
# l=5 | |
# i=0 | |
# j=1 | |
# k=0 | |
# k=1 | |
# k=2 | |
# k=3 | |
# k=4 | |
# i=1 | |
# j=0 | |
# k=0 | |
# k=1 | |
# k=2 | |
# k=3 | |
# k=4 | |
# l=4 | |
# i=0 | |
# j=1 | |
# k=0 | |
# k=1 | |
# k=2 | |
# k=3 | |
# j=2 | |
# k=0 | |
# k=1 | |
# k=2 | |
# k=3 | |
# found match at i=0, j=2 of length=4 | |
# bana | |
# this is closer! the k's look good, BUT notice how the result is WRONG | |
# this is simply because the FIRST time that s[i + k] != s[j + k], we should BREAK. | |
# i know we discussed this despite not coding it on the board | |
# basically what happened is that s[i + k] == s[j + k] where k == l, so the last character matched. | |
# this is why you want to break early whne the characters are not equal | |
# after those changes, we get the result! | |
# l=5 | |
# i=0 | |
# j=1 | |
# k=0 | |
# i=1 | |
# j=0 | |
# k=0 | |
# l=4 | |
# i=0 | |
# j=1 | |
# k=0 | |
# j=2 | |
# k=0 | |
# i=1 | |
# j=0 | |
# k=0 | |
# j=2 | |
# k=0 | |
# i=2 | |
# j=0 | |
# k=0 | |
# j=1 | |
# k=0 | |
# l=3 | |
# i=0 | |
# j=1 | |
# k=0 | |
# j=2 | |
# k=0 | |
# j=3 | |
# k=0 | |
# i=1 | |
# j=0 | |
# k=0 | |
# j=2 | |
# k=0 | |
# j=3 | |
# k=0 | |
# k=1 | |
# k=2 | |
# found match at i=1, j=3 of length=3 | |
# ana | |
# i next went back and added more test examples and added spacing between certain characters |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment