[Leetcode]1456. Maximum Number of Vowels in a Substring of Given Length
2024. 7. 30. 17:12ㆍAlgorithm
1. problem :
https://leetcode.com/problems/maximum-number-of-vowels-in-a-substring-of-given-length/
2. solution 1 :
class Solution:
def maxVowels(self, s: str, k: int) -> int:
vowels = set("aeiou")
current_count = 0
max_count = 0
# initialize window
for i in range(k):
if s[i] in vowels:
current_count += 1
max_count = current_count
for i in range(k,len(s)):
if s[i] in vowels:
current_count += 1
if s[i-k] in vowels:
current_count -= 1
max_count = max(current_count,max_count)
return max_count
sliding window를 이용했다. 나는 word를 더하고 빼고 해서 각각의 word마다 모음 count를 했다. time over가 걸렸다.
그래서 chat gpt 4o한테 물어본 결과 초기윈도를 설정하고, 이후부터는 들어가는 char와 나가는 char가 모음이냐 아니야에 따라 current_count를 갱신하면 된다고 해서 통과했다.
'Algorithm' 카테고리의 다른 글
[Leetcode]128. Longest Consecutive Sequence (0) | 2024.07.31 |
---|---|
[Leetcode]36. Valid Sudoku(x) (0) | 2024.07.31 |
[Leetcode]643. Maximum Average Subarray I (0) | 2024.07.30 |
[Leetcode]792. Number of Matching Subsequences(x) (0) | 2024.07.29 |
[Leetcode]392. Is Subsequence(x) (0) | 2024.07.28 |