15 Jun 2024C++ / Python / TypeScriptEasy

Student Attendance Record I

Collected C++, Python, TypeScript solutions for student attendance record i. Add a dedicated write-up later if you want deeper notes.

auto-generated entry for Student Attendance Record I. the solution files are available below.

solution files

  • C++ student-attendance-record-i/synced-solution.cpp
  • Python student-attendance-record-i/synced-solution.py
  • TypeScript student-attendance-record-i/synced-solution.ts

Solution files

Pythonstudent-attendance-record-i/synced-solution.py
class Solution:
    def checkRecord(self, s: str) -> bool:
        return s.count(class="syntax-string">"A") < class="syntax-number">2 and class="syntax-string">"LLL" not in s
C++student-attendance-record-i/synced-solution.cpp
class Solution {
public:
    bool checkRecord(string s) {
        int absences = class="syntax-number">0;
        int consecutiveLate = class="syntax-number">0;

        for (char c : s) {
            if (c == class="syntax-string">'A') {
                absences++;
                consecutiveLate = class="syntax-number">0;
                if (absences >= class="syntax-number">2) {
                    return false;
                }
            } else if (c == class="syntax-string">'L') {
                consecutiveLate++;
                if (consecutiveLate >= class="syntax-number">3) {
                    return false;
                }
            } else {
                consecutiveLate = class="syntax-number">0;
            }
        }
        return true;
    }
};
TypeScriptstudent-attendance-record-i/synced-solution.ts
function checkRecord(s: string): boolean {
    let absences = class="syntax-number">0;
    let consecutiveLate = class="syntax-number">0;

    for (const ch of s) {
        if (ch === class="syntax-string">"A") {
            absences++;
            consecutiveLate = class="syntax-number">0;
            if (absences >= class="syntax-number">2) {
                return false;
            }
        } else if (ch === class="syntax-string">"L") {
            consecutiveLate++;
            if (consecutiveLate >= class="syntax-number">3) {
                return false;
            }
        } else {
            consecutiveLate = class="syntax-number">0;
        }
    }
    return true;
}