Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Regex working down from max pattern len solution in Clear category for Double Substring by kkkkk
import re
def double_substring(line):
""" Return length of longest non-overlapping repeating substring """
line_len = len(line)
# Search for repeated substrings by starting with the largest
# possible pattern length, which will be no more than half of the
# line length, and working down to a pattern length of 1.
for pattern_len in range(line_len // 2, 0, -1):
# Given a pattern length, search for a match starting from the
# first position in the string to the last possible index,
# which is the line length minus the pattern length.
for idx in range((line_len - pattern_len) + 1):
# The pattern used in the regex starts from the current index
# to the length of the pattern. The search area starts at the
# current index.
if re.search(r'({}).*\1'.format(line[idx:idx+pattern_len]),
line[idx:]):
return pattern_len
return 0
Aug. 24, 2017