Given a string s, return the length of the longest substring that occurs at least twice in s.
If no such repeating substring exists, return 0. The two occurrences are allowed to overlap.
Function Signature
def longest_repeating_substring(s: str) -> int:
Rules
-
A substring is a contiguous block of characters of
s
. Two occurrences count as different when they start at different indices. They may overlap, so in
"aaa"
the substring
"aa"
occurs twice (at indices 0 and 1).
-
Return only the length, as an integer. Return
0
when no substring of length 1 or more appears twice.
Constraints
-
2 <= len(s) <= 2000
-
s
consists of lowercase English letters.
Examples
Input: s = "abcd"
Output: 0
All four letters are different, so not even a single character repeats.
Input: s = "abbaba"
Output: 2
"ab" occurs at indices 0 and 3, and "ba" occurs at indices 2 and 4. The four substrings of length 3 ("abb", "bba", "bab", "aba") are all different, so no length-3 substring repeats.
Input: s = "aaaaa"
Output: 4
"aaaa" starts at index 0 and at index 1. The two occurrences overlap, which is allowed. The whole string of length 5 occurs only once.