Taro Logo

Maximum Number of Vowels in a Substring of Given Length

Medium
2 months ago

Given a string s and an integer k, return the maximum number of vowel letters in any substring of s with length k.

Vowel letters in English are 'a', 'e', 'i', 'o', and 'u'.

For example:

  1. If s = "abciiidef" and k = 3, the output should be 3 because the substring "iii" contains 3 vowel letters.
  2. If s = "aeiou" and k = 2, the output should be 2 because any substring of length 2 contains 2 vowels.
  3. If s = "leetcode" and k = 3, the output should be 2 because "lee", "eet" and "ode" contain 2 vowels.

Write a function that efficiently finds the maximum number of vowel letters in any substring of s with length k. Consider edge cases such as empty strings or when k is larger than the string length. Optimize for time and space complexity.

Sample Answer
## Maximum Number of Vowels in a Substring of Given Length

This problem requires us to find the maximum number of vowel letters within any substring of a given string `s` with a specified length `k`. Let's explore a couple of approaches to solve this problem.

### Naive Approach

We can iterate through all possible substrings of length `k` and count the number of vowels in each substring. We keep track of the maximum vowel count encountered so far.

```python
def max_vowels_naive(s: str, k: int) -> int:
    vowels = "aeiou"
    max_count = 0
    for i in range(len(s) - k + 1):
        count = 0
        for j in range(i, i + k):
            if s[j] in vowels:
                count += 1
        max_count = max(max_count, count)
    return max_count

Example:

s = "abciiidef"
k = 3
print(max_vowels_naive(s, k))

Output: 3

Optimal Solution: Sliding Window

A more efficient approach involves using the sliding window technique. We maintain a window of size k and slide it through the string. We keep track of the number of vowels in the current window. When we move the window, we subtract the vowel count of the character leaving the window and add the vowel count of the character entering the window.

def max_vowels_sliding_window(s: str, k: int) -> int:
    vowels = "aeiou"
    count = 0
    # Initial window
    for i in range(k):
        if s[i] in vowels:
            count += 1
    
    max_count = count
    
    # Slide the window
    for i in range(k, len(s)):
        if s[i] in vowels:
            count += 1
        if s[i - k] in vowels:
            count -= 1
        max_count = max(max_count, count)

    return max_count

Example:

s = "abciiidef"
k = 3
print(max_vowels_sliding_window(s, k))

Output: 3

Big(O) Run-time Analysis

  • Naive Approach: O(n*k), where n is the length of the string s and k is the length of the substring. This is because for each of the n-k+1 possible substrings, we iterate k times to count the vowels.
  • Sliding Window: O(n), where n is the length of the string s. We iterate through the string once.

Big(O) Space Usage Analysis

  • Naive Approach: O(1). We use a constant amount of extra space.
  • Sliding Window: O(1). We use a constant amount of extra space.

Edge Cases

  • Empty string: If the string s is empty, we should return 0.
  • k = 0: If k is 0, we should return 0.
  • k > len(s): If k is greater than the length of s, we should return the number of vowels in s.
  • String with no vowels: If the string contains no vowels, the result should be 0.

Here is the code handling some edge cases:

def max_vowels_sliding_window_edge_cases(s: str, k: int) -> int:
    if not s or k == 0:
        return 0
    
    if k > len(s):
        vowels = "aeiou"
        count = 0
        for char in s:
            if char in vowels:
                count += 1
        return count

    vowels = "aeiou"
    count = 0
    # Initial window
    for i in range(k):
        if s[i] in vowels:
            count += 1
    
    max_count = count
    
    # Slide the window
    for i in range(k, len(s)):
        if s[i] in vowels:
            count += 1
        if s[i - k] in vowels:
            count -= 1
        max_count = max(max_count, count)

    return max_count