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
输入一串字符,如果A的总数大于等于2或连续出现3个或以上L,则返回false;否则返回true。
思路:手动判断前两个字符是否为A,然后从第3个字符开始用循环判断第i-2,i-1,i个字符是否为L,以及当前字符是否为A。
bool checkRecord(string s) {
int a=0,l=0,i;
if(s[0]=='A')a++;
if(s.size()>=2&&s[1]=='A')a++;
if (a>1)return false;
for(i=2;i<s.size();i++)
{
if(s[i]=='A')
{
a++;
if (a>1)return false;
}
else if(s[i-2]=='L'&&s[i-1]=='L'&&s[i]=='L')return false;
}
return true;
}