1456. Maximum Number of Vowels in a Substring of Given Length Medium
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.
Example 4:
Input: s = "rhythms", k = 4
Output: 0
Explanation: We can see that s doesn't have any vowel letters.
Example 5:
Input: s = "tryhard", k = 4
Output: 1
Approach
Input: A string s and a numeric integer interval parameter k
Output: Extract highest possible quantity of vowel characters discovered packed tightly encompassing fixed length dimension subsets
This problem inherently leverages Fixed-length Sliding Window models effectively.
We can establish a standard boundary frame corresponding directly with metric length limits designated by k while continuously referencing active sum variables marking present vowel inclusion instances systematically mapping internal tracking dynamics efficiently.
Upon any unilateral shifts stepping further rightward across string sequences proportionately updating evaluation outcomes subtracting outpaced variables previously evaluated while incorporating consecutive entry properties matching target vowel lists concurrently tracking state levels effectively dynamically comparing running tallies logically replacing top numbers maintaining ultimate peaks observed across complete strings evaluated.
Once fully traversed natively pass back recorded maxima evaluating total constraints.
Implementation
class Solution:
def maxVowels(self, s: str, k: int) -> int:
vowels = set('aeiou') # Reference dictionary isolating character pools targeting mapping lookups cleanly
count = 0 # Numerical state evaluating dynamic metrics bounded within active frame continuously
# Initialization sweep measuring early subsets establishing baseline limits spanning 'k' parameter ranges sequentially
for i in range(0, k):
if s[i] in vowels:
count += 1
ans = count # Define benchmark max quantity recorded capturing current initial evaluations directly bounding limits
# Transition bounds advancing sequences stepping 'k' elements proceeding forwards
for i in range(k, len(s)):
# Assess escaping border properties removing parameters leaving evaluation scope correctly
if s[i - k] in vowels:
count -= 1
# Assess encompassing properties evaluating additional properties appending towards evaluation scope
if s[i] in vowels:
count += 1
# Record maximum comparison replacing metrics tracking highest evaluations achieved
ans = max(ans, count)
return ans # Final determination passed backwards/**
* @param {string} s
* @param {number} k
* @return {number}
*/
var maxVowels = function(s, k) {
const set = new Set(['a', 'e', 'i', 'o', 'u']);
let count = 0;
for (let i = 0; i < k; i++) {
if (set.has(s[i])) {
count++;
}
}
let left = 0;
let ans = count;
for (let j = k; j < s.length; j++) {
if (set.has(s[j])) {
count++;
}
if (set.has(s[left])) {
count--;
}
left++;
ans = Math.max(ans, count);
}
return ans;
};Complexity Analysis
- Time Complexity: O(n) checking components systematically mapping whole string sets
- Space Complexity: O(1)
Links
1456. Maximum Number of Vowels in a Substring of Given Length (English)1456. 定长子串中元音的最大数目 (Chinese)