Simple O(1n) Java solution


  • 0
    U
    class Solution {
        public boolean detectCapitalUse(String word) {
            boolean capital = false;
            int smallCount = 0;
            int capitalCount = 0;
            
            if(Character.isUpperCase(word.charAt(0))) {
                capital = true;
                capitalCount++;
                
            } else {
                smallCount++;
            }
            
            for(int i = 1; i < word.length(); i++) {
                if(Character.isUpperCase(word.charAt(i))) {
                    capitalCount++;
                } else {
                    smallCount++;
                }
                
            }
            if(capitalCount == word.length()) {
                return true;
            }
            
            if (smallCount == word.length()) {
                return true;
            }
            
            if (smallCount == word.length() - 1 && capital == true) {
                return true;
            }
            return false;
            
        }
    }
    

Log in to reply
 

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