Maximum Number of Vowels in a Substring of Given Length

ㅋㅋ·2023년 5월 5일
0

알고리즘-leetcode

목록 보기
133/135

문자열 s에서 k 길이로 만들 수 있는 부분 문자열 중 문자열의 최대 모음 개수를 구하는 문제

class Solution {
public:
    int maxVowels(string s, int k) {
        
        int vowelCount{0};
        int head{0};
        int length = s.size();
        for (; head < k && head < length; ++head)
        {
            if (CheckVowel(s[head]))
            {
                ++vowelCount;
            }
        }

        int maxCount{vowelCount};
        for (int tail = 0; head < length; ++tail, ++head)
        {
            if (CheckVowel(s[tail]))
            {
                --vowelCount;
            }

            if (CheckVowel(s[head]))
            {
                ++vowelCount;
            }

            maxCount = std::max(maxCount, vowelCount);

        }

        return maxCount;
    }

    bool CheckVowel(char c)
    {
        switch (c)
        {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            return true;
        }

        return false;
    }
};

0개의 댓글