Frontend School

Maximum Vowels in a Fixed-Length Substring

DSA
easy

Maximum Vowels in a Fixed-Length Substring

Easy
String
Sliding Window
Arrays

Overview

At Inmobi, optimizing content delivery and user engagement is paramount. Imagine we're analyzing short text snippets, like ad headlines or search queries, to predict their potential impact. A simple yet effective heuristic could be the 'vowel density' within a focused segment. This problem asks you to identify the most 'vowel-rich' segment of a fixed length, which could inform content scoring or A/B testing strategies.

What you need to do / Task

Given a string s consisting of lowercase English letters and an integer k, your task is to find the maximum number of vowel letters in any substring of s that has a length of k.

Input & output

  • Input:
    • s: A string containing only lowercase English letters.
    • k: An integer representing the exact length of the substrings to consider.
  • Output: An integer representing the maximum count of vowels found in any substring of length k.

Clarifications

  • The vowels are 'a', 'e', 'i', 'o', 'u'. Other letters are consonants.
  • The input string s will only contain lowercase English letters.
  • The integer k will always be positive and less than or equal to the length of s.

Examples

1

Example 1

Input:

s = "aeiou", k = 2

Output:

2

Note:

Substrings of length 2 are "ae", "ei", "io", "ou". All of them contain 2 vowels. The maximum is 2.

2

Example 2

Input:

s = "leetcode", k = 3

Output:

2

Note:

Substrings of length 3: "lee" (2 vowels), "eet" (2 vowels), "etc" (1 vowel), "tco" (1 vowel), "cod" (1 vowel), "ode" (2 vowels). The maximum is 2.

3

Example 3

Input:

s = "rhythm", k = 4

Output:

0

Note:

Substrings of length 4: "rhy" (0 vowels), "hyth" (0 vowels), "ythm" (0 vowels). The maximum is 0.

Constraints

Constraints & assumptions

  • 1 <= s.length <= 10^5
  • 1 <= k <= s.length
  • s consists of only lowercase English letters.

Out of scope

  • Handling uppercase letters or non-alphabetic characters in the input string.
  • Complex linguistic analysis; focus purely on the count of specified vowels.

What interviewers evaluate

  • Correctness and robustness of the sliding window implementation.
  • Efficiency of the solution in terms of time and space complexity.
  • Clarity, readability, and maintainability of the code.
  • Ability to handle edge cases within the given constraints.

Loading editor...