My very straightforward solution


  • 0
    M

    Firstly, check the first char. If it is not capital, then the remaining must be all lower case. Otherwise, the remaining can be all uppercase or lower case.

    public boolean detectCapitalUse(String s) {
    	     char first = s.charAt(0);
    	     if('a' <= first && first <= 'z')
    	        return allLowerCase(s.substring(1));
    	     else
    	        return allUpperCase(s.substring(1)) || allLowerCase(s.substring(1));
    	}
    
    	private boolean allUpperCase(String s){
    	    return s.equals(s.toUpperCase());
    	}
    
    	private boolean allLowerCase(String s){
    	    return s.equals(s.toLowerCase());
    	}

Log in to reply
 

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