Student Attendance Record I (LeetCode 551): Solution and Explanation

Easy
String

Each character of s is a day: A absent, L late, P present. Return true if there are fewer than 2 absences in total and never 3 late days in a row. PPALLP is eligible, PPALLL is not.

Intuition

The two rules are different kinds of check. The absence rule is about a total over the whole record, while the late rule is about a run of consecutive days.

A total needs a counter that only goes up. A run needs a counter that goes up on L and drops back to zero on any other day.

Both counters can be updated in the same left-to-right pass, and the moment either one hits its limit the answer is known, so the scan can stop early.

Approach: Two counters with early exit

  1. Set absent = 0 and lateStreak = 0.
  2. For each day: if it is A, add one to absent and return false when it reaches 2.
  3. If it is L, add one to lateStreak and return false when it reaches 3.
  4. Otherwise (A or P) reset lateStreak to 0, because the run of late days is broken.
  5. If the loop finishes, return true.

Step-by-step example

s = "PPALLP"
  1. Days 1 and 2 are P: the late streak stays 0.
  2. Day 3 is A: absences become 1, still below 2. The streak resets to 0.
  3. Day 4 is L: streak 1. Day 5 is L: streak 2.
  4. Day 6 is P: the streak resets to 0.
  5. The scan ends with 1 absence and a longest late run of 2, so the answer is true.

Code: Python, Java, C++

1class Solution:
2    def checkRecord(self, s: str) -> bool:
3        absent = 0
4        late_streak = 0
5        for ch in s:
6            if ch == 'A':
7                absent += 1
8                if absent >= 2:
9                    return False
10            if ch == 'L':
11                late_streak += 1
12                if late_streak >= 3:
13                    return False
14            else:
15                late_streak = 0
16        return True
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each day is read at most once, and the scan may stop early.

Space

O(1)

Two integer counters.

Other approaches

Built-in string checks (time O(n), space O(1))

Return s.count('A') < 2 and 'LLL' not in s. It is short and clear, but it scans the string twice and never stops early.

Regular expression (time O(n), space O(1))

Reject any string matching A.*A|LLL. Correct, but harder to read and slower to run than two counters.

Common mistakes

  • Counting all late days instead of consecutive ones. LLPLLPLL has six late days but is still eligible.
  • Resetting the late streak only on P. An absence also breaks a run of late days.
  • Using absent > 2 or lateStreak > 3. The limits are strict: 2 absences or 3 late days in a row already fail.

Frequently asked questions

Does an absence break a late streak?

Yes. The rule is about late days that are consecutive, so any day that is not L, including A, ends the run.

Why return as soon as a rule fails?

Once the student has 2 absences or 3 late days in a row, nothing later in the record can undo it, so the rest of the scan is wasted work.

What is Student Attendance Record II about?

It asks how many records of length n would be eligible, which turns the same two counters into the states of a dynamic programming solution.