551. Student Attendance Record I
You are given a string representing an attendance record for a student. The record only contains the following three characters:
'A' : Absent.
'L' : Late.
'P' : Present.
A student could be rewarded if his attendance record doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).
You need to return whether the student could be rewarded according to his attendance record.
Example 1:
Input: "PPALLP"
Output: True
Example 2:
Input: "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;
}
bool checkRecord(string s) { // time: O(n); space: O(1)
int A_cnt = 0, L_cnt = 0;
for (const char& ch : s) {
if (ch == 'A') ++A_cnt;
if (ch == 'L') ++L_cnt;
else L_cnt = 0;
if (A_cnt > 1 || L_cnt > 2) return false;
}
return true;
}
Last updated
Was this helpful?