C++, Python Solutions. O(n) runtime, O(1) space.


  • 0

    C++

    bool checkRecord(const string& s) {
        bool A = false;
        short L = 0;
        for(const char& c : s) {
            if(c == 'L') {
                if(L == 2)
                    return false;
                L += 1;
            }
            else {
                if(c == 'A') {
                    if(A)
                        return false;
                    A = true;
                }
                L = 0;
            }
        }
        return true;
    }
    

    Python

    def checkRecord(self, s):
        A = False
        L = 0
        for c in s:
            if c == 'L':
                if L == 2:
                    return False
                L += 1
            else:
                if c == 'A':
                    if A:
                        return False
                    A = True
                L = 0
        return True
    

Log in to reply
 

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