题目:
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
思路:
练手题目。咋感觉这道题目在原来出现过?
代码:
class Solution {
public:
bool checkRecord(string s) {
int max_absent = 0, max_late = 0, index = 0;
while (index < s.length()) {
if (s[index] == 'L') {
int late = 1;
while (++index < s.length() && s[index] == 'L') {
++late;
}
max_late = max(max_late, late);
}
else {
max_absent += s[index] == 'A' ? 1 : 0;
++index;
}
}
return max_absent <= 1 && max_late <= 2;
}
};