551 Student Attendance Record I

Given a string to represent a student's attendance record, this record contains only the following three characters:
    'A' : Absent, absent
    'L' : Late, late
    'P' : Present, present
if a student's attendance record is in No more than one 'A' (absence) and no more than two consecutive 'L' (late), then the student will be rewarded.
You need to determine whether the student will be rewarded based on his attendance record.
Example 1:
Input: "PPALLP"
Output: True

Example 2:
Input: "PPALLL"
Output: False
See: https://leetcode.com/problems/student-attendance-record-i/description/

C++:

method one:

class Solution {
public:
    bool checkRecord(string s)
    {
        int cntA = 0, cntL = 0;
        for (char c : s)
        {
            if (c == 'A')
            {
                if (++cntA > 1)
                {
                    return false;
                }
                cntL = 0;
            }
            else if (c == 'L')
            {
                if (++cntL > 2)
                {
                    return false;
                }
            }
            else
            {
                cntL = 0;
            }
        }
        return true;
    }
};

Method Two:

class Solution {
public:
    bool checkRecord(string s)
    {
        return (s.find("A") == string::npos || s.find("A") == s.rfind("A")) && s.find("LLL") == string::npos;
    }
};

  Reference: http://www.cnblogs.com/grandyang/p/6736484.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324738759&siteId=291194637