551. Student Attendance Record I
Input: "PPALLP"
Output: TrueInput: "PPALLL"
Output: False// Naive
bool checkRecord(string s) { // time: O(n); space: O(1)
int a_cnt = 1, b_continuous_cnt = 2;
for (const char& ch : s) {
if (ch == 'A') {
b_continuous_cnt = 2;
if (--a_cnt < 0) return false;
} else if (ch == 'L') {
if (--b_continuous_cnt < 0) return false;
} else {
b_continuous_cnt = 2;
}
}
return true;
}Last updated