Detect Capital

ㅋㅋ·2023년 1월 2일
0

알고리즘-leetcode

목록 보기
82/135

문자열 데이터를 받고 해당 문자열이 아래의 세 조건 중 하나를 만족하는지 판단하는 문제

All letters in this word are capitals, like "USA".
All letters in this word are not capitals, like "leetcode".
Only the first letter in this word is capital, like "Google".
class Solution {
public:
    bool detectCapitalUse(string word) {
        
        if (word.size() == 1)
        {
            return true;
        }

        if (isupper(word[0]))
        {
            int lower{};
            int upper{};
            for (int i = 1; i < word.size(); i++)
            {
                if (isupper(word[i]))
                {
                    upper++;
                }
                else
                {
                    lower++;
                }

                if (upper != 0 && lower != 0)
                {
                    return false;
                }
            }
        }
        else
        {
            for (int i = 1; i < word.size(); i++)
            {
                if (isupper(word[i]))
                {
                    return false;
                }
            }
        }

        return true;
    }
};

0개의 댓글