JavaScript solution in 1 line


  • 0
    W

    It's easy to think about using Regex in JavaScript. Here are the codes.

    /**
     * @param {string} word
     * @return {boolean}
     */
    var detectCapitalUse = function(word) {
        return /(^[A-Z]+$)|(^[A-Z][a-z]+$)|(^[a-z]+$)/.test(word);
    };
    

    If you are familiar to Regex, it will be a direct solution.


Log in to reply
 

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