Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Stops with first max match solution in Speedy category for Long Repeat Inside by kkkkk
import re
def repeat_inside(line):
""" Return first longest repeating substring """
line_len = len(line)
# Handle the special case where the pattern length is one character
# and the pattern is repeated for the length of the line.
if len(set(line)) == 1:
return 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.
m = re.search(r'({})\1+'.format(line[idx:idx+pattern_len]),
line[idx:])
if m:
return m.group(0)
return ''
Aug. 24, 2017
Comments: