Longest Repeating Substring - Problem
Given a string s, return the length of the longest repeating substring. If no repeating substring exists, return 0.
A repeating substring is a substring that occurs at least twice in the string.
Input & Output
Example 1 — Basic Repeating Pattern
$
Input:
s = "abcabc"
›
Output:
3
💡 Note:
The substring "abc" appears twice: at positions 0-2 and 3-5, so the longest repeating substring has length 3
Example 2 — Single Character Repetition
$
Input:
s = "aabaaaba"
›
Output:
2
💡 Note:
The substring "aa" appears multiple times, and "aaba" appears twice, but "aa" has length 2 which is the maximum
Example 3 — No Repetition
$
Input:
s = "abcdef"
›
Output:
0
💡 Note:
No substring appears more than once, so return 0
Constraints
- 1 ≤ s.length ≤ 1000
- s consists of lowercase English letters
Visualization
Tap to expand
Understanding the Visualization
1
Input
String with potential repeating patterns
2
Process
Find all substrings that appear multiple times
3
Output
Length of the longest repeating substring
Key Takeaway
🎯 Key Insight: Compare the string with shifted versions of itself to efficiently find the longest pattern that repeats
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code