Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Explained solution in Clear category for Double Substring by Selindian
def double_substring(line):
"""
length of the longest substring that non-overlapping repeats more than once.
"""
# your code here
if len(line) < 2: return 0 # We need at least 2 chars
start, end, result = 0, 1, '' # Init variables for start, end and result
while end < len(line): # Loop while end smaller than lenght of line
searchfor = line[start:end] # Compile search string
if line[end:].find(searchfor) != -1: # If Found ...
if len(searchfor) > len(result): # ... if length > old result
result = searchfor # ... ... let that be the new result
end +=1 # ... increase the searchstring by one by adding 1 to end
else: # Else
start += 1 # ... increase the start for searchsting by one
end = start + 1 # ... and set also a new end.
return len(result) # return the lenght of the result.
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert double_substring('aaaa') == 2, "First"
assert double_substring('abc') == 0, "Second"
assert double_substring('aghtfghkofgh') == 3, "Third"
print('"Run" is good. How is "Check"?')
March 6, 2022