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 lengthn that make a student eligible for an attendance award. The answer may be very large, so return it modulo109 + 7.
Example 1:
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).
Problem summary: 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 109 + 7.
Baseline thinking
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Dynamic Programming
Example 1
2
Example 2
1
Example 3
10101
Related Problems
Student Attendance Record I (student-attendance-record-i)
Step 02
Core Insight
What unlocks the optimal approach
No official hints in dataset. Start from constraints and look for a monotonic or reusable state.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03
Algorithm Walkthrough
Iteration Checklist
Define state (indices, window, stack, map, DP cell, or recursion frame).
Apply one transition step and update the invariant.
Record answer candidate when condition is met.
Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04
Edge Cases
Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Largest constraint values
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05
Full Annotated Code
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #552: Student Attendance Record II
class Solution {
private final int mod = (int) 1e9 + 7;
private int n;
private Integer[][][] f;
public int checkRecord(int n) {
this.n = n;
f = new Integer[n][2][3];
return dfs(0, 0, 0);
}
private int dfs(int i, int j, int k) {
if (i >= n) {
return 1;
}
if (f[i][j][k] != null) {
return f[i][j][k];
}
int ans = dfs(i + 1, j, 0);
if (j == 0) {
ans = (ans + dfs(i + 1, j + 1, 0)) % mod;
}
if (k < 2) {
ans = (ans + dfs(i + 1, j, k + 1)) % mod;
}
return f[i][j][k] = ans;
}
}
// Accepted solution for LeetCode #552: Student Attendance Record II
func checkRecord(n int) int {
f := make([][][]int, n)
for i := range f {
f[i] = make([][]int, 2)
for j := range f[i] {
f[i][j] = make([]int, 3)
for k := range f[i][j] {
f[i][j][k] = -1
}
}
}
const mod = 1e9 + 7
var dfs func(i, j, k int) int
dfs = func(i, j, k int) int {
if i >= n {
return 1
}
if f[i][j][k] != -1 {
return f[i][j][k]
}
ans := dfs(i+1, j, 0)
if j == 0 {
ans = (ans + dfs(i+1, j+1, 0)) % mod
}
if k < 2 {
ans = (ans + dfs(i+1, j, k+1)) % mod
}
f[i][j][k] = ans
return ans
}
return dfs(0, 0, 0)
}
# Accepted solution for LeetCode #552: Student Attendance Record II
class Solution:
def checkRecord(self, n: int) -> int:
@cache
def dfs(i, j, k):
if i >= n:
return 1
ans = 0
if j == 0:
ans += dfs(i + 1, j + 1, 0)
if k < 2:
ans += dfs(i + 1, j, k + 1)
ans += dfs(i + 1, j, 0)
return ans % mod
mod = 10**9 + 7
ans = dfs(0, 0, 0)
dfs.cache_clear()
return ans
// Accepted solution for LeetCode #552: Student Attendance Record II
struct Solution;
const MOD: i64 = 1_000_000_007;
impl Solution {
fn check_record(n: i32) -> i32 {
let n = n as usize;
if n == 1 {
return 3;
}
let mut p = vec![0; n + 1];
let mut l = vec![0; n + 1];
p[1] = 1;
l[1] = 1;
p[2] = 2;
l[2] = 2;
for i in 3..=n {
p[i] = l[i - 1] + p[i - 1];
p[i] %= MOD;
l[i] = p[i - 1] + p[i - 2];
l[i] %= MOD;
}
let mut lp = vec![0; n + 1];
lp[0] = 1;
for i in 1..=n {
lp[i] = l[i] + p[i];
lp[i] %= MOD;
}
let mut res: i64 = lp[n];
for i in 0..n {
res += (lp[i]) * (lp[n - 1 - i]);
res %= MOD;
}
res as i32
}
}
#[test]
fn test() {
let n = 2;
let res = 8;
assert_eq!(Solution::check_record(n), res);
}
// Accepted solution for LeetCode #552: Student Attendance Record II
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #552: Student Attendance Record II
// class Solution {
// private final int mod = (int) 1e9 + 7;
// private int n;
// private Integer[][][] f;
//
// public int checkRecord(int n) {
// this.n = n;
// f = new Integer[n][2][3];
// return dfs(0, 0, 0);
// }
//
// private int dfs(int i, int j, int k) {
// if (i >= n) {
// return 1;
// }
// if (f[i][j][k] != null) {
// return f[i][j][k];
// }
// int ans = dfs(i + 1, j, 0);
// if (j == 0) {
// ans = (ans + dfs(i + 1, j + 1, 0)) % mod;
// }
// if (k < 2) {
// ans = (ans + dfs(i + 1, j, k + 1)) % mod;
// }
// return f[i][j][k] = ans;
// }
// }
Step 06
Interactive Study Demo
Use this to step through a reusable interview workflow for this problem.
Press Step or Run All to begin.
Step 07
Complexity Analysis
Time
O(n × m)
Space
O(n × m)
Approach Breakdown
RECURSIVE
O(2ⁿ) time
O(n) space
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
DYNAMIC PROGRAMMING
O(n × m) time
O(n × m) space
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Shortcut: Count your DP state dimensions → that’s your time. Can you drop one? That’s your space optimization.
Coach Notes
Common Mistakes
Review these before coding to avoid predictable interview regressions.
State misses one required dimension
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.