Ctrl K

Every Letter at Least k Times

Medium
You are given a string text of lowercase English letters and a number min_count. Find the longest substring (a contiguous piece of text) in which every letter that appears does so at least min_count times. Return its length, or 0 if there is no such substring.
Example 1
text='xxyzzyq', min_count=2 → 6

In "xxyzzy" every letter occurs exactly twice, while q appears only once and cannot be included. The answer is 6.

Example 2
text='abab', min_count=3 → 0

No letter occurs three times, so no substring qualifies.

Example 3
text='mmmn', min_count=1 → 4

With a threshold of 1 the whole string qualifies.

Constraints
  • 0 ≤ text.length ≤ 10⁴
  • text consists of lowercase English letters
  • 1 ≤ min_count ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsBoth Cases Stretch EasyLongest Repeat-Free Stretch MediumTwo-Letter Stretch MediumRecurring Gene Windows MediumWindow with Few Kinds of Letters Medium
loading Python…
Input
text='xxyzzyq', min_count=2
Expected
6

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