Ctrl K

Window with Few Kinds of Letters

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

The substrings "pqpq" and "qrrq" each use two different letters and have length 4; nothing longer stays within two.

Example 2
text='zzz', limit=0 → 0

Zero different letters allows only the empty substring.

Constraints
  • 0 ≤ text.length ≤ 5 · 10⁴
  • text consists of lowercase English letters
  • 0 ≤ limit ≤ 50
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsLongest Repeat-Free Stretch MediumTwo-Letter Stretch MediumRecurring Gene Windows MediumEvery Letter at Least k Times MediumOne-Color Run After Repaints Medium
loading Python…
Input
text='pqpqrrqp', limit=2
Expected
4

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