> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Student Attendance Record II Python Solution

> Tested Python solution for LeetCode 552 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 552, [Hard](/catalog/hard). Topics: [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/student-attendance-record-ii/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 552   # by problem number
lcpy gen -s student_attendance_record_ii   # by problem name
```

## Problem

An attendance record for a student can be represented as a string where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:

* `'A'`: Absent.
* `'L'`: Late.
* `'P'`: Present.

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

* The student was absent (`'A'`) for **strictly** fewer than 2 days **total**.
* The student was **never** late (`'L'`) for 3 or more **consecutive** days.

Given an integer `n`, return *the **number** of possible attendance records of length* `n` *that make a student eligible for an attendance award*. The answer may be very large, so return it **modulo** `10^9 + 7`.

### Examples

```
Input: n = 2
Output: 8
Explanation: There are 8 records with length 2 that are eligible for an award:
"PP", "AP", "PA", "LP", "PL", "AL", "LA", "LL"
Only "AA" is not eligible because there are 2 absences (there need to be fewer than 2).
```

```
Input: n = 1
Output: 3
```

```
Input: n = 10101
Output: 183236316
```

### Constraints

* 1 \<= n \<= 10^5

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/student_attendance_record_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/student_attendance_record_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def check_record(self, n: int) -> int:
        mod = 1_000_000_007
        # dp[a][l]: records ending with `a` total absences and `l` trailing
        # consecutive lates.
        dp = [[0] * 3 for _ in range(2)]
        dp[0][0] = 1
        for _ in range(n):
            no_absent = sum(dp[0]) % mod
            with_absent = sum(dp[1]) % mod
            nxt = [
                # append 'P' (late streak resets); 'A' starts the streak anew
                [no_absent, dp[0][0], dp[0][1]],
                [(no_absent + with_absent) % mod, dp[1][0], dp[1][1]],
            ]
            dp = nxt
        return (sum(dp[0]) + sum(dp[1])) % mod
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.