Complete-Preparation

šŸŽ‰ One-stop destination for all your technical interview Preparation šŸŽ‰

View the Project on GitHub

551. Student Attendance Record I 🌟

You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:

The student is eligible for an attendance award if they meet both of the following criteria:

Return true if the student is eligible for an attendance award, or false otherwise.

Iterative Solution

class Solution {
public:
    bool checkRecord(string s)
    {
        int n = s.size();
        int aCnt = 0, lCnt = 0;
        for (auto& x : s) {
            if (x == 'L') lCnt++;
            else lCnt = 0;

            if (x == 'A') aCnt++;

            if ((aCnt > 1) || (lCnt >= 3))
                return false;
        }
        return true;
    }
};

Using Builtin Functions

class Solution {
public:
    bool checkRecord(string s)    {
        return !((s.find("LLL") != string::npos) || (count(s.begin(), s.end(), 'A') > 1));
    }
};

Recursive Solution

class Solution {
    bool checkRecordRec(string s, int aCnt, int lCnt, int i) {
        if(i>s.size()) return true;
        if((aCnt>1)||(lCnt>=3)) return false;

        if(s[i]=='A') {aCnt++;lCnt=0;}
        else if(s[i]=='L') lCnt++;
        else lCnt=0;

        return checkRecordRec(s,aCnt,lCnt,i+1);
    }
public:
    bool checkRecord(string s) {
        return checkRecordRec(s,0,0,0);
    }
};