设计 网站访问次数,网页设计旅游网站源代码免费,洛可可设计公司现状,找项目1. 题目
给你字符串 s 和整数 k 。
请返回字符串 s 中长度为 k 的单个子字符串中可能包含的最大元音字母数。
英文中的 元音字母 为#xff08;a, e, i, o, u#xff09;。
示例 1#xff1a;
输入#xff1a;s abciiidef, k 3
输出#xff1a;3
解释a, e, i, o, u。
示例 1
输入s abciiidef, k 3
输出3
解释子字符串 iii 包含 3 个元音字母。示例 2
输入s aeiou, k 2
输出2
解释任意长度为 2 的子字符串都包含 2 个元音字母。示例 3
输入s leetcode, k 3
输出2
解释lee、eet 和 ode 都包含 2 个元音字母。示例 4
输入s rhythms, k 4
输出0
解释字符串 s 中不含任何元音字母。示例 5
输入s tryhard, k 4
输出1提示
1 s.length 10^5
s 由小写英文字母组成
1 k s.length来源力扣LeetCode 链接https://leetcode-cn.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 2. 解题
先找到一个大小 k 的窗口然后向右滑动
class Solution {
public:int maxVowels(string s, int k) {int count 0, maxcount 0, l 0, r 0;while(r k){if(s[r]a||s[r]e||s[r]i||s[r]o||s[r]u)count;r;}maxcount count;for( ; r s.size(); l,r){if(s[r]a||s[r]e||s[r]i||s[r]o||s[r]u)count;if(s[l]a||s[l]e||s[l]i||s[l]o||s[l]u)count--;maxcount max(maxcount, count);}return maxcount;}
};72 ms 9.9 MB