[LeetCode] 520. Detect Capital - 문자열c++

ha·2022년 1월 24일
0

LeetCode

목록 보기
10/21

C++풀이
-capital1 = 가장 첫번째 글자 / capital2=두번째 글자 대문자 여부 판정
-조건1)처음, 다음 모두 대문자 / 조건2)처음 대문자, 두번째 소문자 / 조건3)둘 다 소문자

class Solution {
public:
    bool detectCapitalUse(string word) {
        bool capital1=false;
        bool capital2=false;
        
        if(word[0]>='A' && word[0]<='Z') capital1 = true;
        if(word[1]>='A' && word[1]<='Z') capital2 = true;
        if(!capital1 && capital2) return false;
        
        for(int i=2;i<word.size();i++){
            if(capital1 && capital2){
                if(word[i]>='A' && word[i]<='Z') continue;
                else return false;
            }
            else if(capital1 && !capital2){
                if(word[i]>='A' && word[i]<='Z') return false;
            }
             else if(!capital1 && !capital2){
                  if(word[i]>='A' && word[i]<='Z') return false;
             }   
        }
        return true;
        
    }
};

0개의 댓글