Student Attendance Record I - Problem

Imagine you're a teacher evaluating students for the perfect attendance award at the end of the school year! ๐ŸŽ“

You have a student's attendance record represented as a string s, where each character tells you what happened on that day:

  • 'A' - Absent (student didn't come to school)
  • 'L' - Late (student arrived after the bell)
  • 'P' - Present (student was on time)

To win the attendance award, a student must meet both criteria:

  1. ๐Ÿšซ No more than 1 absence - They can miss at most 1 day total
  2. โฐ Never late 3+ days in a row - No streaks of 3 or more consecutive late days

Return true if the student qualifies for the award, false otherwise.

Example: "PPALLP" โ†’ true (1 absence, max 2 consecutive lates)
"PPALLL" โ†’ false (3 consecutive lates = disqualified!)

Input & Output

example_1.py โ€” Basic Valid Case
$ Input: s = "PPALLP"
โ€บ Output: true
๐Ÿ’ก Note: The student has 1 absence (โ‰ค 1 allowed) and maximum 2 consecutive lates (< 3 allowed). Both conditions satisfied.
example_2.py โ€” Too Many Consecutive Lates
$ Input: s = "PPALLL"
โ€บ Output: false
๐Ÿ’ก Note: Although only 0 absences, the student has 3 consecutive lates which violates the attendance policy.
example_3.py โ€” Too Many Absences
$ Input: s = "ALAPPLA"
โ€บ Output: false
๐Ÿ’ก Note: The student has 2 absences, which exceeds the maximum allowed (1). The consecutive late condition is satisfied but overall ineligible.

Constraints

  • 1 โ‰ค s.length โ‰ค 1000
  • s[i] is either 'A', 'L', or 'P'
  • Case sensitive: Only uppercase letters are valid

Visualization

Tap to expand
๐ŸŽ“ Smart Attendance Award System๐Ÿ” Administrator's Checklistโœ“ Count total absences (limit: โ‰ค 1)โœ“ Track consecutive late streaks (limit: < 3)Real-time Processing: "PPALLP"[Processing animation showing day-by-day evaluation]P โ†’ Present โœ“ | Counters: absent=0, late_streak=0P โ†’ Present โœ“ | Counters: absent=0, late_streak=0A โ†’ Absent โš ๏ธ | Counters: absent=1, late_streak=0 (reset)L โ†’ Late โฐ | Counters: absent=1, late_streak=1L โ†’ Late โฐ | Counters: absent=1, late_streak=2 (getting close!)๐Ÿ† Result: AWARD GRANTED!Final: 1 absence (โ‰ค1 โœ“) | Max 2 consecutive lates (<3 โœ“)โšก Efficiency: O(n) time, O(1) space with early termination
Understanding the Visualization
1
Start the Review
Begin with clean counters: 0 absences, 0 consecutive lates
2
Daily Assessment
For each day, update your mental notes and check if limits are exceeded
3
Immediate Decision
Stop as soon as you find a violation - no need to continue checking
4
Award Verdict
If you finish without violations, the student gets the award!
Key Takeaway
๐ŸŽฏ Key Insight: We can solve both conditions simultaneously in one pass, making immediate decisions without storing historical data!
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
42.3K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen