Solutions in C++, Java, Golang, Javascript and Python!


  • 0
    R

    Python:

    class Solution(object):
        def detectCapitalUse(self, word):
            lower = 0
            upper = 0
            for letter in word:
                if letter.isupper() and lower > 0:
                    return False
                elif letter.islower() and upper > 1:
                    return False
                elif letter.islower():
                    lower += 1
                elif letter.isupper():
                    upper += 1
            return True 
    

    Java version:

    public boolean detectCapitalUse(String word) {
            int lower = 0, upper = 0;
            for(char letter : word.toCharArray()) {
                if(Character.isUpperCase(letter) && lower > 0)
                    return false;
                else if (Character.isLowerCase(letter) && upper > 1) 
                    return false;
                else if(Character.isLowerCase(letter)) 
                    lower++;
                else if(Character.isUpperCase(letter)) 
                    upper++;
            }
            return true; 
        }
    

    Javascript version:

    var detectCapitalUse = function(word) {
        var lower = 0, upper = 0;
            for(var letter = 0; letter < word.length; letter++) {
                if(word[letter].toUpperCase() == word[letter] && lower > 0)
                    return false;
                else if (word[letter].toLowerCase() == word[letter] && upper > 1) 
                    return false;
                else if(word[letter].toLowerCase() == word[letter]) 
                    lower++;
                else if(word[letter].toUpperCase() == word[letter]) 
                    upper++;
            }
            return true; 
    };
    

    C++ version:

    bool detectCapitalUse(string word) {
            int lower = 0, upper = 0;
            for(int letter = 0; letter < word.size(); letter++) {
                if(isupper(word[letter]) && lower > 0)
                    return false;
                else if (islower(word[letter]) && upper > 1) 
                    return false;
                else if(islower(word[letter])) 
                    lower++;
                else if(isupper(word[letter])) 
                    upper++;
            }
            return true; 
        }
    

    Golang version:

    import (
         		"fmt"
         		"unicode"
        )
    
    func detectCapitalUse(word string) bool {
        lower := 0
        upper := 0
        for _, letter := range word {
            if(unicode.IsUpper(letter) && lower > 0) {
                return false
            } else if (unicode.IsLower(letter) && upper > 1) {
                return false
            } else if(unicode.IsLower(letter)) {
                lower++
            } else if(unicode.IsUpper(letter)) {
                upper++
            }
        }
        return true
    }
    

Log in to reply
 

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