1318. Quiet Letter Window

Easy1.5 points estimatedSliding WindowStringTwo Pointers

A subtitle editor reviews a line of lowercase text s and wants the longest passage that is not too loud. Loudness is measured by vowels, and the vowels are exactly the letters a, e, i, o and u. A passage is a contiguous substring of s, and it is acceptable when it contains at most k vowels.

Return the length of the longest acceptable substring. If k is 0 the passage may contain consonants only, and the answer can be 0 when every letter is a vowel. Grow the right end of a window one letter at a time and move the left end forward whenever the vowel count exceeds k. Aim for O(n) time and O(1) extra space.

Example 1

Input:
s = "abcde", k = 1
Output:
4
Explanation:

The substring 'bcde' has one vowel (e), and the whole string has two, so the answer is 4.

Example 2

Input:
s = "aeiou", k = 0
Output:
0
Explanation:

Every letter is a vowel, so no non-empty substring is quiet and the answer is 0.

Constraints

1 ≤ s.length ≤ 1000

0 ≤ k ≤ s.length

s consists of lowercase English letters

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(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…