Question 15 of 75 - LeetCode Weekly Challenge

LeetCode Challenge #1456. Maximum Number of Vowels in a Substring of Given Length

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'.

 

Example 1:

Input: s = "abciiidef", k = 3
Output: 3
Explanation: The substring "iii" contains 3 vowel letters.

Example 2:

Input: s = "aeiou", k = 2
Output: 2
Explanation: Any substring of length 2 contains 2 vowels.

Example 3:

Input: s = "leetcode", k = 3
Output: 2
Explanation: "lee", "eet" and "ode" contain 2 vowels.

 

Constraints:

  • 1 <= s.length <= 105
  • s consists of lowercase English letters.
  • 1 <= k <= s.length
Video Solution
C++ Solution
				
					class Solution {
public:
// Function to find the maximum number of vowels in a substring of length k
    int maxVowels(string s, int k) {
        // Variable to keep track of the count of vowels in the current substring
        int count = 0;
        // String containing all the vowels
        string vowels = "aeiou";

        // Loop to count vowels in the first substring of length k
        for (int i = 0; i < k; i++) {
            // Check if the current character is a vowel
            if (vowels.find(s[i]) != string::npos)
                count++;
        }
        
        // Variables for sliding window approach
        int j = 0;      // Start of the current substring
        int maxx = count;  // Variable to store the maximum count of vowels

        // Loop to slide the window through the string and update the count of vowels
        for (int i = k; i < s.length(); i++) {
            // If the character at the start of the current substring is a vowel, decrease the count
            if (vowels.find(s[j]) != string::npos)
                count--;
            
            // If the character at the end of the current substring is a vowel, increase the count
            if (vowels.find(s[i]) != string::npos)
                count++;

            j++;  // Move the window one position to the right

            // Update the maximum count of vowels encountered so far
            maxx = max(maxx, count);

            // If the maximum count equals k, no need to continue searching, return k
            if (maxx == k)
                return maxx;
        }

        // Return the maximum count of vowels found in any substring
        return maxx;
    }
};

				
			
Code Explanation

The purpose of this function is to find the maximum number of vowels in a substring of length ‘k’ in a given string ‘s’. Let’s see the step-by-step approach towards the solution:

1. First, let’s initialize a count variable (int count = 0). Think of it as our trusty counter for the number of vowels.

2. Next, we’ll need a string containing all the vowels – “aeiou”. Now, here comes the fun part: we’ll use the sliding window technique. Imagine it like a little window sliding over the string, peeking at each part to perform our vowel count magic.

3. Time for a ‘for’ loop! This loop runs ‘k’ times, focusing on counting vowels in the initial substring of length ‘k’. Inside this loop, if we find ‘s[i]’ in our vowel string (meaning it’s a vowel), we give our count variable a little boost (count++).

4. We also need to track the maximum number of vowels we encounter. For this, we introduce ‘maxx’.

5. Now, let’s set up a second ‘for’ loop starting from position ‘k’ and stretching to the end of the string. Here’s the plan inside this loop:
– If we’re removing a vowel (bye-bye vowel), we decrease our count (count–).
– If we’re adding a vowel (hello there!), we increase our count (count++).
– We then play a little game of comparison to see if the current count of vowels is the highest we’ve seen. If it is, ‘maxx’ gets updated to this new high score (maxx = max(maxx, count)).

6. This loop keeps on trucking until it has graciously examined all possible substrings of length ‘k’. In the end, we triumphantly return the highest number of vowels found in any of these substrings.

And there you have it – a neatly packaged function to find the maximum number of vowels in a substring of length ‘k’ in a given string ‘s’.

Happy Coding with edSlash