Javascript solution O(n) space O(1)


  • 0
    R
    var detectCapitalUse = function(word) {
        var asciiCode,
            capitalNum = 0;
        
        for(var i = 0; i < word.length; i++){
            asciiCode = word[i].charCodeAt(0);
            if(asciiCode >= 65 && asciiCode <= 90){
                capitalNum++;
            }
        }
        
        if(capitalNum > 1 && capitalNum < word.length)
            return false;
        if(capitalNum === 1 && word[0].charCodeAt(0) > 90)
            return false
        return true;
    };

Log in to reply
 

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