leetcode 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
class Solution {
public:
    bool checkRecord(string s) {
        int a = 0;
        int b = 0;
        int len = 1;
        if (s == "") return true;
        if (s[0] == 'A') a++;
        for (int i = 1; i < s.size(); ++i) {
            if (s[i] == 'A') a++;
            if (s[i] == s[i-1] && s[i] == 'L') {
                len ++;
            } else {
                b = max(len, b);
                len = 1;
            }
        }
        b = max(len, b);
        if (a <= 1 && b <= 2) return true;
        return false;
    }
};
posted on 2018-03-01 09:14  Beserious  阅读(116)  评论(0编辑  收藏  举报