"Student Attendance Record II" is the significantly harder follow-up to the first problem. You are asked to find the total number of possible attendance strings of length n that would be eligible for an award. Since the number can be very large, you must return it modulo 10^9 + 7. The eligibility rules remain: fewer than 2 'A's and no 3 consecutive 'L's. This is no longer a validation problem; it's a counting problem.
This is a classic "Hard" dynamic programming problem asked by companies like Sprinklr and Google. It tests your ability to define complex DP states and identify transitions. Because n can be large, it also provides an opportunity for candidates to discuss optimization techniques like Matrix Exponentiation (which can solve this in O(log n) time). It separates candidates who can only do simple DP from those who can manage multiple interacting constraints.
This problem uses Dynamic Programming with State Compression. The state needs to track three things:
n x 2 x 3. The transitions involve adding 'P', 'A', or 'L' to the current state and seeing which new state we land in. For example, if we have one 'A' and two 'L's, we cannot add another 'L', but we can add 'P'.For n=2, possible eligible strings are:
Failing to apply the modulo operation at every addition step is a very frequent error that leads to integer overflow. Another mistake is not correctly defining the state, such as forgetting to track the "total 'A's" separately from the "consecutive 'L's". Candidates also sometimes struggle with the base cases (n=0 or n=1) which are essential for the DP to work correctly.
Practice defining the DP state clearly. Write down the transitions for each character ('P', 'A', 'L') explicitly: