JavaScript solution


  • 0
    G
    const validations = {
        lowercase: /[a-z]/,
        uppercase: /[A-Z]/
    };
    
    /**
     * @param {string} word
     * @return {boolean}
     */
    var detectCapitalUse = function(word) {
        let validation = null;
        const letters = word.split('');
        
        if (validations.lowercase.test(letters[0])) {
            validation = 'lowercase';
        } else {
            validation = 'uppercase';
        }
        
        if (letters.length < 2) {
            return true;
        }
        
        if (validation === 'uppercase' && validations.lowercase.test(letters[1])) {
            validation = 'lowercase';
        }
        
        return letters.slice(1).every((letter) => validations[validation].test(letter));
    };
    

Log in to reply
 

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