KatInterview

552. Student Attendance Record II

HardSolutions 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 for the number of valid attendance records of length n. An attendance record is a string composed of 'A' (Absent), 'L' (Late), and 'P' (Present). A record is considered valid if it satisfies two conditions:

Solution

Student Attendance Record II in Python

class Solution:
    def checkRecord(self, n: int) -> int:
        MOD = 10**9 + 7

        # dp[j][k] represents the number of valid sequences of a certain length
        # with j 'A's and k consecutive trailing 'L's.
        # We use space optimization, so this represents the counts for the current length.
        dp = [[0] * 3 for _ in range(2)]

        # Base case: For a string of length 0 (the empty string), 
        # there is one way: 0 'A's, 0 trailing 'L's.
        dp[0][0] = 1

        # Iterate from length 1 to n
        for _ in range(n):
            new_dp = [[0] * 3 for _ in range(2)]

            # --- Case: 0 'A's --- #
            # Add 'P': can be appended to any record with 0 'A's. Resets trailing 'L's to 0.
            new_dp[0][0] = (dp[0][0] + dp[0][1] + dp[0][2]) % MOD
            # Add 'L': can be appended to a record with 0 'A's and 0 trailing 'L's.
            new_dp[0][1] = dp[0][0]
            # Add 'L': can be appended to a record with 0 'A's and 1 trailing 'L'.
            new_dp[0][2] = dp[0][1]

            # --- Case: 1 'A' --- #
            # Add 'A': can be appended to any record with 0 'A's. Total 'A's becomes 1.
            from_A0 = (dp[0][0] + dp[0][1] + dp[0][2]) % MOD
            # Add 'P': can be appended to any record with 1 'A'. Total 'A's remains 1.
            from_A1 = (dp[1][0] + dp[1][1] + dp[1][2]) % MOD
            new_dp[1][0] = (from_A0 + from_A1) % MOD

            # Add 'L': can be appended to a record with 1 'A' and 0 trailing 'L's.
            new_dp[1][1] = dp[1][0]
            # Add 'L': can be appended to a record with 1 'A' and 1 trailing 'L'.
            new_dp[1][2] = dp[1][1]
            
            # Move to the next length
            dp = new_dp

        # The total number of valid records of length n is the sum of all states.
        total = 0
        for j in range(2):
            for k in range(3):
                total = (total + dp[j][k]) % MOD
        
        return total

Student Attendance Record II in Java

class Solution {
    public int checkRecord(int n) {
        int MOD = 1_000_000_007;

        // dp[j][k] represents the number of valid sequences of a certain length
        // with j 'A's and k consecutive trailing 'L's.
        // We use space optimization, so this represents the counts for the current length.
        long[][] dp = new long[2][3];

        // Base case: For a string of length 0 (the empty string), 
        // there is one way: 0 'A's, 0 trailing 'L's.
        dp[0][0] = 1;

        // Iterate from length 1 to n
        for (int i = 1; i <= n; i++) {
            long[][] newDp = new long[2][3];

            // --- Case: 0 'A's --- //
            // Add 'P': can be appended to any record with 0 'A's. Resets trailing 'L's to 0.
            newDp[0][0] = (dp[0][0] + dp[0][1] + dp[0][2]) % MOD;
            // Add 'L': can be appended to a record with 0 'A's and 0 trailing 'L's.
            newDp[0][1] = dp[0][0];
            // Add 'L': can be appended to a record with 0 'A's and 1 trailing 'L'.
            newDp[0][2] = dp[0][1];

            // --- Case: 1 'A' --- //
            // Add 'A': can be appended to any record with 0 'A's. Total 'A's becomes 1.
            long fromA0 = (dp[0][0] + dp[0][1] + dp[0][2]) % MOD;
            // Add 'P': can be appended to any record with 1 'A'. Total 'A's remains 1.
            long fromA1 = (dp[1][0] + dp[1][1] + dp[1][2]) % MOD;
            newDp[1][0] = (fromA0 + fromA1) % MOD;

            // Add 'L': can be appended to a record with 1 'A' and 0 trailing 'L's.
            newDp[1][1] = dp[1][0];
            // Add 'L': can be appended to a record with 1 'A' and 1 trailing 'L'.
            newDp[1][2] = dp[1][1];
            
            // Move to the next length
            dp = newDp;
        }

        // The total number of valid records of length n is the sum of all states.
        long total = 0;
        for (int j = 0; j < 2; j++) {
            for (int k = 0; k < 3; k++) {
                total = (total + dp[j][k]) % MOD;
            }
        }

        return (int) total;
    }
}

Student Attendance Record II in C++

class Solution {
public:
    int checkRecord(int n) {
        int MOD = 1e9 + 7;

        // dp[j][k] represents the number of valid sequences of a certain length
        // with j 'A's and k consecutive trailing 'L's.
        // We use space optimization, so this represents the counts for the current length.
        vector<vector<long>> dp(2, vector<long>(3, 0));

        // Base case: For a string of length 0 (the empty string), 
        // there is one way: 0 'A's, 0 trailing 'L's.
        dp[0][0] = 1;

        // Iterate from length 1 to n
        for (int i = 1; i <= n; ++i) {
            vector<vector<long>> new_dp(2, vector<long>(3, 0));

            // --- Case: 0 'A's --- //
            // Add 'P': can be appended to any record with 0 'A's. Resets trailing 'L's to 0.
            new_dp[0][0] = (dp[0][0] + dp[0][1] + dp[0][2]) % MOD;
            // Add 'L': can be appended to a record with 0 'A's and 0 trailing 'L's.
            new_dp[0][1] = dp[0][0];
            // Add 'L': can be appended to a record with 0 'A's and 1 trailing 'L'.
            new_dp[0][2] = dp[0][1];

            // --- Case: 1 'A' --- //
            // Add 'A': can be appended to any record with 0 'A's. Total 'A's becomes 1.
            long from_A0 = (dp[0][0] + dp[0][1] + dp[0][2]) % MOD;
            // Add 'P': can be appended to any record with 1 'A'. Total 'A's remains 1.
            long from_A1 = (dp[1][0] + dp[1][1] + dp[1][2]) % MOD;
            new_dp[1][0] = (from_A0 + from_A1) % MOD;

            // Add 'L': can be appended to a record with 1 'A' and 0 trailing 'L's.
            new_dp[1][1] = dp[1][0];
            // Add 'L': can be appended to a record with 1 'A' and 1 trailing 'L'.
            new_dp[1][2] = dp[1][1];
            
            // Move to the next length
            dp = new_dp;
        }

        // The total number of valid records of length n is the sum of all states.
        long total = 0;
        for (int j = 0; j < 2; ++j) {
            for (int k = 0; k < 3; ++k) {
                total = (total + dp[j][k]) % MOD;
            }
        }

        return (int)total;
    }
};

Student Attendance Record II in TypeScript

function checkRecord(n: number): number {
    const MOD = 1_000_000_007;

    // dp[j][k] represents the number of valid sequences of a certain length
    // with j 'A's and k consecutive trailing 'L's.
    // We use space optimization, so this represents the counts for the current length.
    let dp: number[][] = Array(2).fill(0).map(() => Array(3).fill(0));

    // Base case: For a string of length 0 (the empty string), 
    // there is one way: 0 'A's, 0 trailing 'L's.
    dp[0][0] = 1;

    // Iterate from length 1 to n
    for (let i = 1; i <= n; i++) {
        const newDp: number[][] = Array(2).fill(0).map(() => Array(3).fill(0));

        // --- Case: 0 'A's --- //
        // Add 'P': can be appended to any record with 0 'A's. Resets trailing 'L's to 0.
        newDp[0][0] = (dp[0][0] + dp[0][1] + dp[0][2]) % MOD;
        // Add 'L': can be appended to a record with 0 'A's and 0 trailing 'L's.
        newDp[0][1] = dp[0][0];
        // Add 'L': can be appended to a record with 0 'A's and 1 trailing 'L'.
        newDp[0][2] = dp[0][1];

        // --- Case: 1 'A' --- //
        // Add 'A': can be appended to any record with 0 'A's. Total 'A's becomes 1.
        const fromA0 = (dp[0][0] + dp[0][1] + dp[0][2]) % MOD;
        // Add 'P': can be appended to any record with 1 'A'. Total 'A's remains 1.
        const fromA1 = (dp[1][0] + dp[1][1] + dp[1][2]) % MOD;
        newDp[1][0] = (fromA0 + fromA1) % MOD;

        // Add 'L': can be appended to a record with 1 'A' and 0 trailing 'L's.
        newDp[1][1] = dp[1][0];
        // Add 'L': can be appended to a record with 1 'A' and 1 trailing 'L'.
        newDp[1][2] = dp[1][1];
        
        // Move to the next length
        dp = newDp;
    }

    // The total number of valid records of length n is the sum of all states.
    let total = 0;
    for (let j = 0; j < 2; j++) {
        for (let k = 0; k < 3; k++) {
            total = (total + dp[j][k]) % MOD;
        }
    }

    return total;
}

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.