LeetCode 551: Student Attendance Record I Solution in Python – A Step-by-Step Guide
Imagine you’re a teacher reviewing a student’s attendance record—like "PPALLP"—where each letter tracks their daily status (Present, Absent, Late), and your task is to decide if they deserve an award by checking if they’ve been absent too often or tardy too many times in a row, such as ensuring fewer than 2 absences and no 3 consecutive lates. That’s the neat challenge of LeetCode 551: Student Attendance Record I, an easy-level problem that’s a fantastic way to practice string processing in Python. We’ll explore two solutions: the Best Solution, a single-pass with counters that’s efficient and straightforward, and an Alternative Solution, a regular expression check that’s concise but less intuitive for beginners. With detailed examples, clear code, and a friendly tone—especially for the single-pass method—this guide will help you award that badge, whether you’re new to coding or brushing up. Let’s check that record and start counting!
What Is LeetCode 551: Student Attendance Record I?
In LeetCode 551: Student Attendance Record I, you’re given a string s representing a student’s attendance record, where each character is either 'A' (Absent), 'L' (Late), or 'P' (Present), and your task is to return True if the student qualifies for an attendance award, and False otherwise. The award criteria are: fewer than 2 absences ('A') and no 3 consecutive lates ('L'). For example, with s = "PPALLP", the student has 1 'A' and no "LLL", so they qualify (True). This problem builds on LeetCode 344: Reverse String for basic string handling but focuses on counting and sequence checking.
Problem Statement
- Input: s (str)—attendance record with 'A', 'L', 'P'.
- Output: Boolean—True if award-worthy (less than 2 'A', no 3 consecutive 'L'), False otherwise.
- Rules: Check absences < 2 and no "LLL" substring.
Constraints
- 1 <= s.length <= 1000
- s[i] is 'A', 'L', or 'P'.
Examples
- Input: s = "PPALLP"
- Output: True
- 1 'A' (less than 2), no "LLL".
- Input: s = "PPALLL"
- Output: False
- 1 'A', but "LLL" present.
- Input: s = "PPAA"
- Output: False
- 2 'A' (too many), no "LLL".
Understanding the Problem: Checking Attendance
To solve LeetCode 551: Student Attendance Record I in Python, we need to verify two conditions in a string: the number of 'A's must be less than 2, and there must be no substring "LLL" (3 consecutive 'L's). A naive approach might scan the string multiple times, but with lengths up to 1000, we can optimize with a single pass or pattern matching. The problem tests basic string iteration and condition checking. We’ll explore:
- Best Solution (Single-Pass with Counters): O(n) time, O(1) space—fast and simple.
- Alternative Solution (Regular Expression Check): O(n) time, O(1) space—short but regex-heavy.
Let’s dive into the single-pass solution with a friendly breakdown!
Best Solution: Single-Pass with Counters
Why Single-Pass Wins
The single-pass with counters solution is the best for LeetCode 551 because it checks both conditions—absence count and consecutive lates—in O(n) time and O(1) space by scanning the string once, using simple counters to track 'A's and current 'L' streaks. It’s like flipping through a roll call sheet, ticking off absences and watching for tardy streaks, all in one go!
How It Works
Think of this as an attendance roll checker:
- Step 1: Initialize Counters:
- absent_count for total 'A's.
- late_streak for current consecutive 'L's.
- Step 2: Scan String:
- For each character:
- 'A': Increment absent_count, reset late_streak.
- 'L': Increment late_streak, check if ≥ 3.
- 'P': Reset late_streak.
- Step 3: Check Conditions:
- Return True if absent_count < 2 and no late_streak ≥ 3, else False.
- Why It Works:
- Single pass catches all conditions.
- Constant space keeps it lean.
It’s like an attendance rule enforcer!
Step-by-Step Example
Example: s = "PPALLP"
- Init: absent_count = 0, late_streak = 0, max_late = 0.
- Step 1: Scan:
- 'P': absent_count = 0, late_streak = 0.
- 'P': absent_count = 0, late_streak = 0.
- 'A': absent_count = 1, late_streak = 0.
- 'L': absent_count = 1, late_streak = 1.
- 'L': absent_count = 1, late_streak = 2, max_late = 2.
- 'P': absent_count = 1, late_streak = 0.
- Step 2: Check:
- absent_count = 1 < 2, max_late = 2 < 3.
- Result: True.
Example: s = "PPALLL"
- Scan:
- 'P': absent_count = 0, late_streak = 0.
- 'P': absent_count = 0, late_streak = 0.
- 'A': absent_count = 1, late_streak = 0.
- 'L': absent_count = 1, late_streak = 1.
- 'L': absent_count = 1, late_streak = 2.
- 'L': absent_count = 1, late_streak = 3, max_late = 3.
- Check: absent_count = 1 < 2, but max_late = 3 ≥ 3, fail.
- Result: False.
Code with Detailed Line-by-Line Explanation
Here’s the Python code, explained for beginners:
class Solution:
def checkRecord(self, s: str) -> bool:
# Step 1: Initialize counters
absent_count = 0
late_streak = 0
max_late = 0
# Step 2: Scan string
for char in s:
if char == 'A':
absent_count += 1
late_streak = 0 # Reset late streak
elif char == 'L':
late_streak += 1
max_late = max(max_late, late_streak)
else: # 'P'
late_streak = 0
# Step 3: Check conditions
return absent_count < 2 and max_late < 3
- Lines 4-7: Set up counters for absences and late streaks.
- Lines 10-17: Iterate string:
- 'A': Increment absences, reset late streak.
- 'L': Increment late streak, update max.
- 'P': Reset late streak.
- Line 20: Return True if both conditions met.
- Time Complexity: O(n)—single pass through string.
- Space Complexity: O(1)—few variables.
It’s like an attendance badge giver!
Alternative Solution: Regular Expression Check
Why an Alternative Approach?
The regular expression check uses pattern matching to verify the conditions in O(n) time and O(1) space, offering a concise solution by counting 'A's and checking for "LLL" with regex functions. It’s short but requires regex knowledge, making it a good alternative for pattern-matching fans or concise-code lovers.
How It Works
Picture this as an attendance pattern spotter:
- Step 1: Count 'A's using string count.
- Step 2: Check for "LLL" with regex or string find.
- Step 3: Return True if conditions met.
It’s like an attendance regex inspector!
Step-by-Step Example
Example: s = "PPALLP"
- Step 1: s.count('A') = 1 < 2.
- Step 2: "LLL" not in s (True).
- Result: True.
Example: s = "PPAA"
- Step 1: s.count('A') = 2 ≥ 2, fail.
- Step 2: "LLL" not in s (True), but already failed.
- Result: False.
Code for Regex Approach
class Solution:
def checkRecord(self, s: str) -> bool:
# Step 1: Count absences
absent_count = s.count('A')
# Step 2: Check for 3 consecutive lates
has_three_lates = "LLL" in s
# Step 3: Return result
return absent_count < 2 and not has_three_lates
- Line 4: Count 'A's with built-in method.
- Line 7: Check "LLL" substring.
- Line 10: Return True if both pass.
- Time Complexity: O(n)—string count and substring check.
- Space Complexity: O(1)—no extra space.
It’s a regex attendance checker!
Comparing the Two Solutions
- Single-Pass (Best):
- Pros: O(n), O(1), clear logic.
- Cons: Slightly more code.
- Regex (Alternative):
- Pros: O(n), O(1), concise.
- Cons: Regex learning curve.
Single-pass wins for clarity!
Additional Examples and Edge Cases
- "L": True (0 'A', 1 'L').
- "LLL": False (0 'A', 3 'L').
- "AA": False (2 'A', 0 'L').
Single-pass handles them all!
Complexity Recap
- Single-Pass: Time O(n), Space O(1).
- Regex: Time O(n), Space O(1).
Single-pass’s the beginner’s champ!
Key Takeaways
- Single-Pass: Counter clarity—learn at Python Basics!
- Regex: Pattern power.
- Strings: Attendance is fun.
- Python: Loops or regex, your pick!
Final Thoughts: Award That Badge!
LeetCode 551: Student Attendance Record I in Python is a fun string challenge. Single-pass with counters is your fast track, while regex offers a slick shortcut. Want more? Try LeetCode 344: Reverse String or LeetCode 680: Valid Palindrome II. Ready to check? Head to Solve LeetCode 551 on LeetCode and award that badge today!