Ctrl K

Each Letter At Most Twice

Easy
You are given a string text. Find the longest substring (a contiguous piece of the string) in which no letter appears more than twice, and return its length.
Example 1
text='abacaba' → 5

The substring "bacab" (length 5) has two a's, two b's and one c. Nothing longer works: "abacab" already holds three a's.

Example 2
text='zzzz' → 2

Any three letters in a row are three z's, so the answer is 2.

Example 3
text='xyz' → 3
Constraints
  • 2 ≤ len(text) ≤ 100
  • text consists only of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBoth Cases Stretch EasyTriples Without Repeats EasyLongest Repeat-Free Stretch MediumWord Blocks in Any Order HardShortest Covering Window Hard
loading Python…
Input
text='abacaba'
Expected
5

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