Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Long Repeat Inside by vlad.bezden
"""Long Running Inside.
You should find a repeating sequence inside the substring.
for example: in a string "abababc" - "ab"
is a sequence that repeats more than once, so the answer will be "ababab"
Input: String.
Output: String.
"""
def repeat_inside(line: str) -> str:
"""First the longest repeating substring."""
repeats = []
i = 0
for i, c in enumerate(line):
if (j := line.find(c, i + 1)) == -1:
continue
repeating = 0
subs = line[i:j]
while line[j + len(subs) * repeating:].startswith(subs):
repeating += 1
if repeating:
repeats.append(subs * (repeating + 1))
return max(repeats, key=len) if len(repeats) else ""
if __name__ == "__main__":
assert repeat_inside("aaaaa") == "aaaaa", "First"
assert repeat_inside("aabbff") == "aa", "Second"
assert repeat_inside("aababcc") == "abab", "Third"
assert repeat_inside("abc") == "", "Forth"
assert repeat_inside("abcabcabab") == "abcabc", "Fifth"
Jan. 19, 2020