Skip to content

Commit 3a75090

Browse files
author
Adam Lin
committed
Add gitignore and 1456
1 parent 15c7759 commit 3a75090

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode/

1456_maxVowels.cpp

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include <iostream>
2+
#include <string>
3+
using namespace std;
4+
5+
// class Solution {
6+
// public:
7+
// int maxVowels(string s, int k) {
8+
// int temp = 0, max = 0, start = 0, end = start + k - 1;
9+
// string s2;
10+
// for(int i = 0; i < s.size(); i++){
11+
// s2 = s.substr(start, k);
12+
13+
// if(i >= k - 1){
14+
// for(int j = 0; j < k; j++){
15+
// if(isVowel(s2[j])){
16+
// temp++;
17+
// }
18+
// if(temp > max){
19+
// max = temp;
20+
// }
21+
// }
22+
// temp = 0;
23+
// start++;
24+
// }
25+
// }
26+
// return max;
27+
// }
28+
29+
// bool isVowel(char c){
30+
// return c == 'a' || c == 'e'|| c == 'i'|| c == 'o' || c == 'u';
31+
// }
32+
// };
33+
34+
// Time Limit Exceeded
35+
// 103 / 107 testcases passed
36+
// submitted at Oct 09, 2024 20:18
37+
38+
class Solution {
39+
public:
40+
int maxVowels(string s, int k) {
41+
int temp = 0, res = 0, ptrL = 0;
42+
43+
for(int i = 0; i < s.size(); i++){
44+
if(isVowel(s[i])){
45+
temp++;
46+
}
47+
48+
if(i - ptrL + 1 > k){
49+
if(isVowel(s[ptrL])){
50+
temp--;
51+
}
52+
ptrL++;
53+
}
54+
res = max(temp, res);
55+
}
56+
return res;
57+
}
58+
59+
bool isVowel(char c){
60+
return c == 'a' || c == 'e'|| c == 'i'|| c == 'o' || c == 'u';
61+
}
62+
};
63+
64+
int main(){
65+
//string s = "aeiou";
66+
string s = "abciiidef";
67+
int k = 3;
68+
Solution sol;
69+
int result = sol.maxVowels(s, k);
70+
cout << result;
71+
return 0;
72+
}

0 commit comments

Comments
 (0)