Student Attendance Record I (LeetCode 551): Solution and Explanation
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
- Set
absent = 0andlateStreak = 0. - For each day: if it is
A, add one toabsentand return false when it reaches 2. - If it is
L, add one tolateStreakand return false when it reaches 3. - Otherwise (
AorP) resetlateStreakto 0, because the run of late days is broken. - If the loop finishes, return true.
Step-by-step example
s = "PPALLP"
- Days 1 and 2 are
P: the late streak stays 0. - Day 3 is
A: absences become 1, still below 2. The streak resets to 0. - Day 4 is
L: streak 1. Day 5 isL: streak 2. - Day 6 is
P: the streak resets to 0. - 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 True1class Solution {
2 public boolean checkRecord(String s) {
3 int absent = 0;
4 int lateStreak = 0;
5 for (char ch : s.toCharArray()) {
6 if (ch == 'A') {
7 absent++;
8 if (absent >= 2) {
9 return false;
10 }
11 }
12 if (ch == 'L') {
13 lateStreak++;
14 if (lateStreak >= 3) {
15 return false;
16 }
17 } else {
18 lateStreak = 0;
19 }
20 }
21 return true;
22 }
23}1class Solution {
2public:
3 bool checkRecord(string s) {
4 int absent = 0;
5 int lateStreak = 0;
6 for (char ch : s) {
7 if (ch == 'A') {
8 absent++;
9 if (absent >= 2) {
10 return false;
11 }
12 }
13 if (ch == 'L') {
14 lateStreak++;
15 if (lateStreak >= 3) {
16 return false;
17 }
18 } else {
19 lateStreak = 0;
20 }
21 }
22 return true;
23 }
24};Time and space complexity
O(n)
Each day is read at most once, and the scan may stop early.
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.
LLPLLPLLhas 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 > 2orlateStreak > 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.