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
分析:
根据学生的出勤记录来判断是否该给其奖励,条件如下:缺席次数(A)不能超过1次,连续迟到次数(L)不能超过2次。遍历字符串中的字符,变量num1来记录字符'A'的个数,num2记录字符'L'连续出现的次数,num1大于1或num2大于2返回false,否则返回true。
class Solution {
public:
bool checkRecord(string s) {
int num1 = 0;
int num2 = 0;
for(char c : s)
{
if(c == 'A')
{
num1++;
num2 = 0;
if(num1 > 1)
return false;
}
else if(c == 'L')
{
num2++;
if(num2 > 2)
return false;
}
else
num2 = 0;
}
return true;
}
};