주어진 길이의 substring 상 vowels 개수 구하기
class Solution {
public int maxVowels(String s, int k) {
int left = 0;
int len = s.length();
int right = k - 1;
// count first
int count = 0;
for(int i = left; i <= right; i++) {
if (isVowels(s.charAt(i))) {
count += 1;
}
}
int max = count;
while(right < len - 1) {
left += 1;
right += 1;
if (isVowels(s.charAt(left - 1))) {
count -= 1;
}
if (isVowels(s.charAt(right))) {
count += 1;
}
if (count > max) {
max = count;
if (max == k) {
break;
}
}
}
return max;
}
public boolean isVowels(char c) {
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
return true;
}
return false;
}
}
'코딩테스트' 카테고리의 다른 글
[leetcode] 1493. Longest Subarray of 1's After Deleting One Element (0) | 2024.06.25 |
---|---|
[leetcode] 1004. Max Consecutive One III (0) | 2024.06.25 |
[leetcode] 2336. Smallest Number in Infinite Set (0) | 2024.06.18 |
[leetcode] 216. Combination Sum III (0) | 2024.06.15 |
[leetcode] 994. Rotting Oranges (0) | 2024.06.14 |