Student Attendance Record II

Hard

Description

An attendance record is a string with only L, A, P characters. A student could be rewarded if there is at most one A and no more than two continuous L's. Given n, return the number of possible attendance records of length n that make a student eligible for reward.

Examples

Input:n = 2
Output:8
Explanation:

PP, AP, PA, LP, PL, AL, LA, LL are valid.

Input:n = 1
Output:3
Explanation:

For length 1, the valid records are P, A, and L. All three satisfy the constraints: at most one A and no more than two continuous L's.

Input:n = 3
Output:19
Explanation:

For length 3, all valid combinations must be counted. Invalid records include those with 2+ A's (like AAP, AAL) or 3+ continuous L's (LLL). Valid records include PPP, PPA, PAP, APP, PLP, LPP, and others. The total count is 19.

Constraints

  • 1 ≤ n ≤ 10^5

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!