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
Longest Repeating Substring: "abcabc"a b c a b c012345"abc""abc"Position 0-2Position 3-5Found repeating substring "abc" with length 3Output: 3
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
Asked in
Google 25 Amazon 20 Microsoft 15 Facebook 12
35.0K Views
Medium Frequency
~25 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen