LeetCode - 502. Detect Capital(string)

YAMAMAMO·2023년 1월 3일
0

LeetCode

목록 보기
95/100

문제

We define the usage of capitals in a word to be right when one of the following cases holds:

  • 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".
    Given a string word, return true if the usage of capitals in it is right.

https://leetcode.com/problems/detect-capital/description/

Example 1:

Input: word = "USA"
Output: true

Example 2:

Input: word = "FlaG"
Output: false

풀이

class Solution {
    public boolean detectCapitalUse(String word) {
        if(word.length()==1) return true;
        boolean res = true;
        char[] c = word.toCharArray();
        boolean first = Character.isUpperCase(c[0]);
        boolean second = Character.isUpperCase(c[1]);

        for(int i=2; i<c.length; i++){
            if(first&&second&&Character.isUpperCase(c[i])){

            }else if(first&&!second&&!Character.isUpperCase(c[i])){

            }else if(!first&&!second&&!Character.isUpperCase(c[i])){

            }else {
                res = false;
                break;
            };
        }

        if(!first&&second) return false;
        return res;
    }
}
profile
안드로이드 개발자

0개의 댓글