Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Build-a-regex solution in Clear category for Double Substring by JamesArruda
import re
"""
Build a regex that searches for repeating strings of given lengths (since we can't
ask it to search for the longest first on its own). Start with the larger lengths
first for efficiency.
"""
def double_substring(line):
"""
length of the longest substring that non-overlapping repeats more than once.
"""
# substrings can only repeat if they are half or less the length of 'line'
for l in range(len(line)//2, 0, -1):
# search for any set of characters of some length: .{num, }
# capture it: (.{num, })
# allow some or no characters after, not greedy: (.{num, }).*?
# and find the first capture repeated: (.{num, }).*?\1
searcher = r"(.{" + repr(l) + r",}).*?\1"
ans = re.findall(searcher, line)
if ans:
return l
return 0
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"?')
Sept. 21, 2018
Comments: