3ms Straightforward C++ solution


  • 0
    V
    class Solution {
    public:
        bool checkRecord(string s) {
            bool can_have_one_more_A = true;
            int Lcount = 0;
            for (auto ch : s)
                switch (ch) {
                    case 'P':
                        Lcount = 0;
                        break;
                    case 'A':
                        if (!can_have_one_more_A) return false;
                        can_have_one_more_A = false;
                        Lcount = 0;
                        break;
                    case 'L':
                        if (Lcount == 2) return false;
                        Lcount++;
                        break;
                }
            return true;
        }
    };
    

Log in to reply
 

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