Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Using regex and max() with key solution in Clear category for Double Substring by H0r4c3
import re
def double_substring(line):
"""
length of the longest substring that non-overlapping repeats more than once.
"""
pattern = re.compile(r'(.+)(?=.*\1)')
list_of_subs = pattern.findall(line)
if not list_of_subs:
return 0
else:
return len(max(list_of_subs, key=lambda m: len(m)))
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 24, 2022
Comments: