Student Attendance Record I - Problem

You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:

  • 'A': Absent
  • 'L': Late
  • 'P': Present

The student is eligible for an attendance award if they meet both of the following criteria:

  • The student was absent ('A') for strictly fewer than 2 days total
  • The student was never late ('L') for 3 or more consecutive days

Return true if the student is eligible for an attendance award, or false otherwise.

Input & Output

Example 1 — Basic Valid Case
$ Input: s = "PPALLP"
Output: true
💡 Note: The student has 1 absence (< 2) and at most 2 consecutive lates (< 3), so they are eligible for the award.
Example 2 — Too Many Absences
$ Input: s = "PPALLL"
Output: false
💡 Note: The student has 3 consecutive lates, which violates the attendance policy.
Example 3 — Multiple Absences
$ Input: s = "ALPPLA"
Output: false
💡 Note: The student has 2 absences, which is not strictly fewer than 2, so they are not eligible.

Constraints

  • 1 ≤ s.length ≤ 1000
  • s[i] is either 'A', 'L', or 'P'

Visualization

Tap to expand
Student Attendance Record I INPUT Attendance Record String s = "PPALLP" P 0 P 1 A 2 L 3 L 4 P 5 P = Present A = Absent L = Late Award Criteria: Absent less than 2 days Never 3+ consecutive L ALGORITHM STEPS 1 Initialize Counters absences=0, consec_L=0 2 Scan Each Character Single pass through string 3 Update Counters A: absences++ L: consec_L++ P: reset consec_L=0 4 Check Conditions absences less than 2? consec_L less than 3? Tracking Progress: P: abs=0, L=0 P: abs=0, L=0 A: abs=1, L=0 L: abs=1, L=1 L: abs=1, L=2 P: abs=1, L=0 FINAL RESULT Final Counts: Total Absences: 1 1 is less than 2 -- OK Max Consecutive L: 2 2 is less than 3 -- OK OUTPUT true OK Student is eligible for attendance award! Key Insight: Single Pass O(n) Solution: Track two counters while scanning - total absences and consecutive lates. Reset consecutive late counter to 0 whenever we see 'P' or 'A'. Return false early if any condition fails, otherwise return true. Time: O(n), Space: O(1) - optimal solution using constant extra space. TutorialsPoint - Student Attendance Record I | Single Pass - Optimal Time Complexity: O(n) | Space Complexity: O(1)
Asked in
Google 12 Amazon 8 Microsoft 5
28.5K Views
Medium Frequency
~15 min Avg. Time
892 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