KatInterview

551. Student Attendance Record I

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to validate a student's attendance record based on two specific criteria to determine if they are eligible for an award. The record is a string composed of 'A' (Absent), 'L' (Late), and 'P' (Present).

Solution

Student Attendance Record I in Python

class Solution:
    def checkRecord(self, s: str) -> bool:
        # One-liner solution for conciseness, though a loop is also efficient.
        # return s.count('A') < 2 and 'LLL' not in s

        absent_count = 0  # Counts total 'A's
        consecutive_late_count = 0  # Counts consecutive 'L's

        for char in s:
            if char == 'A':
                absent_count += 1
                consecutive_late_count = 0  # 'A' resets late streak
            elif char == 'L':
                consecutive_late_count += 1
            else:  # char == 'P'
                consecutive_late_count = 0  # 'P' resets late streak

            # Check for violation conditions
            if absent_count >= 2 or consecutive_late_count >= 3:
                return False  # Early exit if a rule is broken

        # If the loop completes, both conditions are met
        return True

Student Attendance Record I in Java

class Solution {
    public boolean checkRecord(String s) {
        int absentCount = 0; // Counts total 'A's
        int consecutiveLateCount = 0; // Counts consecutive 'L's

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == 'A') {
                absentCount++;
                consecutiveLateCount = 0; // 'A' resets late streak
            } else if (c == 'L') {
                consecutiveLateCount++;
            } else { // c == 'P'
                consecutiveLateCount = 0; // 'P' resets late streak
            }

            // Check for violation conditions
            if (absentCount >= 2 || consecutiveLateCount >= 3) {
                return false; // Early exit if a rule is broken
            }
        }

        // If the loop completes, both conditions are met
        return true;
    }
}

Student Attendance Record I in C++

class Solution {
public:
    bool checkRecord(string s) {
        int absent_count = 0; // Counts total 'A's
        int consecutive_late_count = 0; // Counts consecutive 'L's

        for (char c : s) {
            if (c == 'A') {
                absent_count++;
                consecutive_late_count = 0; // 'A' resets late streak
            } else if (c == 'L') {
                consecutive_late_count++;
            } else { // c == 'P'
                consecutive_late_count = 0; // 'P' resets late streak
            }

            // Check for violation conditions
            if (absent_count >= 2 || consecutive_late_count >= 3) {
                return false; // Early exit if a rule is broken
            }
        }

        // If the loop completes, both conditions are met
        return true;
    }
};

Student Attendance Record I in TypeScript

function checkRecord(s: string): boolean {
    let absentCount = 0; // Counts total 'A's
    let consecutiveLateCount = 0; // Counts consecutive 'L's

    for (const char of s) {
        if (char === 'A') {
            absentCount++;
            consecutiveLateCount = 0; // 'A' resets late streak
        } else if (char === 'L') {
            consecutiveLateCount++;
        } else { // char === 'P'
            consecutiveLateCount = 0; // 'P' resets late streak
        }

        // Check for violation conditions
        if (absentCount >= 2 || consecutiveLateCount >= 3) {
            return false; // Early exit if a rule is broken
        }
    }

    // If the loop completes, both conditions are met
    return true;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.