

class Solution {
public int maxVowels(String s, int k) {
if (s ==null || s.length() ==0 || s.length() <k) {
return 0;
}
int res =0;
HashSet<Character> set = new HashSet<>();
set.add('a');
set.add('e');
set.add('i');
set.add('o');
set.add('u');
int count =0;
// 从第一个滑动窗口中统计元音字母的数量
for (int i=0;i<k;i++) {
if (set.contains(s.charAt(i))) {
count++;
}
}
res =Math.max(res,count);
// 统计接下来的原因字母的数量 加上j处的元素 减掉j-k处的元素 此时j是从k开始的
for (int j=k;j<s.length();j++) {
char ch =s.charAt(j-k);
char ch1 =s.charAt(j);
if (set.contains(ch)) {
count--;
}
if (set.contains(ch1)) {
count++;
}
res =Math.max(res,count);
}
return res;
}
}
这篇博客探讨了如何解决LeetCode上的问题1456,目标是在给定长度的子串中找到最多数量的元音。文章详细解释了解题思路,包括如何识别元音,以及使用何种算法来有效地遍历字符串并计算元音数量。
961

被折叠的 条评论
为什么被折叠?



