Intuitive Solution


  • 0
    public class Solution {
        public boolean detectCapitalUse(String word) {
            if (word == null || word.length() <= 1) return true;
            boolean firstCapital = true;
            firstCapital = Character.isUpperCase(word.charAt(0));
            if (!firstCapital && Character.isUpperCase(word.charAt(1))) return false; // case: aA
            boolean afterFirst = true;
            for (int i = 2; i < word.length(); i++) {
                afterFirst &= (Character.isUpperCase(word.charAt(i - 1)) ^ (Character.isLowerCase(word.charAt(i))));
            }
            return afterFirst;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.