java solution without built-in function


  • 0
    B
    public boolean detectCapitalUse(String word) {
        if(word == null | word.length() <= 1) {return true;}
        char[] arr = word.toCharArray();
        if(arr[0] - 'a' < 0){
            if(arr[1] - 'a' < 0){
                for(int i = 2; i < arr.length; ++i){
                    if(arr[i] - 'a' >= 0){
                        return false;
                    }
                }
            }
            else{
                for(int i = 2; i < arr.length; ++i){
                    if(arr[i] - 'a' < 0){
                        return false;
                    }
                }
            }
        }
        else{
            for(int i = 1; i < arr.length; ++i){
                if(arr[i] - 'a' < 0){
                    return false;
                }
            }
        }
        return true;
    }

Log in to reply
 

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