c++ solution, use FSM


  • 0
    C

    '''
    bool detectCapitalUse(string word) {
    if (word.empty()) return true;

        int checking = 0;
        for (int i=0; i<word.length(); i++) {
            if (i == 0) {
                if (word[i] >= 65 && word[i] <= 90) {
                    checking = 4;
                } else {
                    checking = 2;
                }
            } else {
                if (word[i] >= 65 && word[i] <= 90) {
                    if (checking == 3 || checking == 2) {
                        return false;
                    } else if (checking == 4) {
                        checking = 1;
                    }
                } else {
                    if (checking == 4) {
                        checking = 3;
                    } else if (checking == 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.