My Java Solution


  • 0
    N
        public boolean detectCapitalUse(String word) {
            boolean firstChar=Character.isUpperCase(word.charAt(0));
            boolean lastChar=Character.isUpperCase(word.charAt(word.length()-1));
           int caps=firstChar?1:0;
            for(int i=1;i<word.length();i++){
                 if(!firstChar&&word.charAt(i)>='A'&&word.charAt(i)<='Z')
                         return false;
                if(firstChar&&word.charAt(i)>='A'&&word.charAt(i)<='Z'){
                  caps++;
                }else if(firstChar&&!(word.charAt(i)>='A'&&word.charAt(i)<='Z')){
                    if(lastChar||caps>1)
                    return false;
                }
                
            }
          
         return true;
        }
    }

Log in to reply
 

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