Sponsored
Sponsored
This approach uses the sliding window technique to efficiently find the maximum number of vowels in any substring of length k.
We first calculate the number of vowels in the initial window of length k. Then, for each subsequent character, we slide the window by one character to the right: we remove the character that goes out of the window and add the one that comes into the window, updating our vowel count accordingly.
This ensures that we examine each character in the string only once, leading to an O(n) time complexity.
Time Complexity: O(n), where n is the length of the string. We iterate through the string with a constant number of operations for each character.
Space Complexity: O(1), as we use a fixed amount of extra space.
1#include <iostream>
2#include <string>
3#include <unordered_set>
4using namespace std;
5
6bool isVowel(char ch) {
7 static unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u'};
8 return vowels.count(ch) > 0;
9}
10
11int maxVowels(const string &s, int k) {
12 int maxCount = 0, currentCount = 0;
13 int n = s.size();
14
for (int i = 0; i < k; ++i) {
if (isVowel(s[i])) {
++currentCount;
}
}
maxCount = currentCount;
for (int i = k; i < n; ++i) {
currentCount += isVowel(s[i]) - isVowel(s[i - k]);
maxCount = max(maxCount, currentCount);
}
return maxCount;
}
int main() {
string s = "abciiidef";
int k = 3;
cout << maxVowels(s, k) << endl;
return 0;
}
In C++, an unordered set is used to define vowels for a faster lookup. The algorithm checks the initial substring of length k for vowels, then uses a loop from k to the end of the string to adjust the vowel count by examining the newly added character and removing the influence of the character leaving the window, updating the maximum vowels found.
Another way to tackle this problem is to use a combination of prefix sums and binary search to precompute regions of vowels.
First, we establish a prefix sum array that accumulates the number of vowels encountered at each character index in the string. Then, we use a binary search within this prefix sum to efficiently find the maximum number of vowels in any moving window of length k.
This approach is more complex and is generally less optimal than the sliding window approach but provides an alternative methodology.
Time Complexity: O(n), for prefix sum construction and analysis.
Space Complexity: O(n), for the prefix sum array.
1#include <stdio.h>
2#include <stdbool.h>
3#include <string.h>
4
5bool isVowel(char ch) {
6 return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
7}
8
9int maxVowels(char * s, int k) {
10 int length = strlen(s);
11 int prefix[length + 1];
12 prefix[0] = 0;
13
14 for (int i = 0; i < length; ++i) {
15 prefix[i + 1] = prefix[i] + isVowel(s[i]);
16 }
17
18 int maxCount = 0;
19 for (int i = k; i <= length; ++i) {
20 maxCount = prefix[i] - prefix[i - k] > maxCount ? prefix[i] - prefix[i - k] : maxCount;
21 }
22
23 return maxCount;
24}
25
26int main() {
27 char s[] = "abciiidef";
28 int k = 3;
29 printf("%d\n", maxVowels(s, k));
30 return 0;
31}
The C solution creates a prefix sum array to accumulate the number of vowels found up to each point in the string. Using this prefix sum, the maximum number of vowels in any contiguous substring of length k can be determined by calculating the difference between pairs of prefix sums separated by k indices.