Ctrl K

Two-Letter Stretch

Medium
You are given a string text. Find the longest substring (a contiguous piece of text) that contains at most two different letters, and return its length.
Example 1
text='kkokmmo' → 4

The substring "kkok" uses just k and o and has length 4.

Example 2
text='abcabc' → 2

Any three letters in a row are all different here, so no stretch is longer than 2.

Constraints
  • 0 ≤ text.length ≤ 10⁵
  • text consists of lowercase English letters
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumRecurring Gene Windows MediumWindow with Few Kinds of Letters MediumEvery Letter at Least k Times MediumOne-Color Run After Repaints Medium
loading Python…
Input
text='kkokmmo'
Expected
4

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