530. Frequent Enough Letters

A radio operator receives a message s made of lowercase letters. A stretch of consecutive characters of the message is called readable if every letter that appears in the stretch appears at least k times within that same stretch.

Return the length of the longest readable stretch of s. If no non-empty stretch is readable, return 0. Note that a letter that does not appear in a stretch imposes no condition on it.

Example 1

Input:
s = "mmmnnoopppp", k = 3
Output:
4
Explanation:

Counting letters: m=3, n=2, o=2, p=4. The longest stretch with every letter at least 3 times is pppp (length 4).

Example 2

Input:
s = "kkjmmmnn", k = 2
Output:
5
Explanation:

The stretch mmmnn has m three times and n twice, so every letter appears at least 2 times; its length is 5.

Constraints

1 ≤ s.length ≤ 105

1 ≤ k ≤ 105

s consists of lowercase English letters only

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Time per case
Python 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(26 * n)
Space
O(1)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…