Skip to content
MediumSliding WindowAI interview only

Find Longest Special Substring That Occurs Thrice

Asked atgoogleamazonmeta

01 · Problem

You are given a string s of lowercase English letters. A string is special if it consists of a single character repeated one or more times (for example "a", "zzz").

Return the length of the longest special substring that occurs at least three times in s. Occurrences are counted by starting position and are allowed to overlap. Return -1 if no special substring occurs three or more times.

02 · Examples

Example 01
Input
s = "bbbcbbb"
Output
2

"bb" occurs twice in each "bbb" block, 4 times in total. "bbb" occurs only twice, once per block.

Example 02
Input
s = "abcdef"
Output
-1

Every character appears once, so no special substring occurs three times.

Example 03
Input
s = "abcaba"
Output
1

"a" occurs at indices 0, 3 and 5. No longer special substring repeats three times.

03 · Constraints

  • 013 <= s.length <= 5 * 105
  • 02s consists only of lowercase English letters
  • 03Occurrences may overlap; return -1 if no special substring occurs at least three times

04 · Optimal complexity

Time
O(n)
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.