본문 바로가기
Algorithm/LeetCode

[009]Detect Capital

by BAYABA 2020. 8. 8.

 

문제: https://leetcode.com/explore/challenge/card/august-leetcoding-challenge/549/week-1-august-1st-august-7th/3409/


주어진 문자열이 조건에 맞는 문자열인지 확인하는 문제입니다.

 

주어진 조건은 3가지 입니다.

1. 모두 대문자

2. 모두 소문자

3. 첫 글자만 대문자, 나머지 소문자


class Solution {
    public boolean detectCapitalUse(String word) {
        String lowerCase = word.toUpperCase();
        String upperCase = word.toLowerCase();

        if (word.isEmpty()) return false;
        else if (word.compareTo("") == 0) return false;

        if (word.compareTo(lowerCase) == 0) return true;
        else if (word.compareTo(upperCase) == 0) return true;
        else {
            String subStr = word.substring(1);
            String subLowerCase = subStr.toLowerCase();
            if (subStr.compareTo(subLowerCase) == 0) return true;
        }

        return false;
    }
}

'Algorithm > LeetCode' 카테고리의 다른 글

[169]Majority Element  (0) 2020.09.18
[136]SingleNumber  (0) 2020.09.18
[008]Find All Duplicates in an Array  (0) 2020.08.07
[007]Add and Search Word - Data structure design  (0) 2020.08.06
[006]Valid Palindrome  (0) 2020.08.03