Simple Java Solution


  • 0
    V

    '''
    public class Solution {
    public boolean detectCapitalUse(String word) {
    boolean result = true ; ;
    // all caps
    if(isCap(word.charAt(0))){

    //If one character of upper case
    if(word.length() == 1 ){
    result = true ;
    return result ;
    }
    //if more than one character
    if(word.length() > 1){

    //first char upper case next all lower case
    if(isCap(word.charAt(0)) && !isCap(word.charAt(1))){
    for(int i = 1 ; i <word.length() ; i ++){
    if(!isCap(word.charAt(i))){
    result = true ;
    }
    else{
    result = false ;
    return result ;
    }
    }
    }
    //all characters Upper case
    if(isCap(word.charAt(0)) && isCap(word.charAt(1))){
    for(int i = 0 ; i < word.length() ; i ++ ){
    if(isCap(word.charAt(i))){
    result = true ;
    }
    else {
    result = false ;
    return result ;
    }
    }
    }
    }

        }
    

    //all characters are small
    if(!isCap(word.charAt(0))){
    for(int i = 0 ; i < word.length() ; i ++){
    if(!isCap(word.charAt(i))){
    result = true ;
    }
    else {
    result = false ;
    return result ;
    }
    }
    }
    return result ;
    }

    //function to see if a character is capital
    public static boolean isCap(char x){
    if(x >= 65 && x <= 90){
    return true ;
    }
    else {
    return false ;
    }
    }
    }
    '''


Log in to reply
 

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