Ctrl K

A Single-Letter Piece Seen Three Times

Medium
You are given a string text of lowercase English letters. Call a substring (a contiguous non-empty piece) single-letter if all its characters are the same, such as "kkk" or "q", but not "kq". Find the length of the longest single-letter substring that occurs in text at least three times. Occurrences may overlap: in "bbbb" the substring "bb" occurs three times, starting at positions 0, 1 and 2. If no single-letter substring occurs three times, return -1.
Example 1
text='xxxyxxyx' → 2

"x" occurs 6 times and "xx" exactly three times: twice inside "xxx" and once in the middle. "xxx" occurs only once, so the answer is 2.

Example 2
text='mnoonm' → -1

Each letter appears only twice, so no single-letter substring occurs three times.

Example 3
text='ttttttt' → 5
Constraints
  • 3 ≤ len(text) ≤ 50
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsA Single-Letter Piece Seen Three Times: Long Text MediumEvery Letter the Same Number of Times MediumTriples Without Repeats EasyLongest Repeat-Free Stretch MediumTwo-Letter Stretch Medium
loading Python…
Input
text='xxxyxxyx'
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 90 tests on the server, hidden ones included.