Student Attendance Record II - 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:

  1. The student was absent ('A') for strictly fewer than 2 days total
  2. 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.

Input & Output

Example 1 — Small Case
$ Input: n = 2
Output: 8
💡 Note: Valid records: PP, PL, PA, LP, LL, LA, AP, AL. Invalid: AA (2 absences), no records with LLL since n=2.
Example 2 — Single Day
$ Input: n = 1
Output: 3
💡 Note: All single-character records are valid: A, L, P. None violate the constraints.
Example 3 — Larger Case
$ Input: n = 10101
Output: 183236316
💡 Note: For large n, the number of valid records grows exponentially but must be computed modulo 10^9 + 7.

Constraints

  • 1 ≤ n ≤ 105

Visualization

Tap to expand
Student Attendance Record II: Count Valid RecordsInputn = 2Constraints< 2 A, no LLLOutput8Valid Records (8):PPPLPALPLLLAAPALInvalid Record (1):AA2 absencesDynamic Programming efficiently counts without generating strings
Understanding the Visualization
1
Input
Length n of attendance record
2
Constraints
< 2 absences, no 3+ consecutive late days
3
Output
Count of valid records modulo 10^9 + 7
Key Takeaway
🎯 Key Insight: Track state as (absences, consecutive_late) to ensure constraints while building records
Asked in
Google 15 Microsoft 8
28.5K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen