Java short solution using built-in String methods


  • 12
    N

    The string can be correctly capitalized if either: it's shorter than 2 characters, or if it's all lower case, or if it's all upper case, or if from position 1 onward there are only lowercase letters.

    public boolean detectCapitalUse(String word) {
            if (word.length() < 2) return true;
            if (word.toUpperCase().equals(word)) return true;
            if (word.substring(1).toLowerCase().equals(word.substring(1))) return true;
            return false;
    }
    

  • 0
    G

    very nice! :--)


  • 0
    C

    Your solution is easy to understand and it`s short.Thank you.


Log in to reply
 

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