Watch 10 video solutions for Student Attendance Record I, a easy level problem involving String. This walkthrough by Amell Peralta has 1,707 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
'A') for strictly fewer than 2 days total.'L') for 3 or more consecutive days.Return true if the student is eligible for an attendance award, or false otherwise.
Example 1:
Input: s = "PPALLP" Output: true Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.
Example 2:
Input: s = "PPALLL" Output: false Explanation: The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.
Constraints:
1 <= s.length <= 1000s[i] is either 'A', 'L', or 'P'.Problem Overview: You receive a string representing a student's attendance record. Each character is 'A' (Absent), 'L' (Late), or 'P' (Present). The record is valid if it contains fewer than two 'A' characters and never has three consecutive 'L' characters.
Approach 1: Single Pass Check (O(n) time, O(1) space)
Scan the string once while tracking two things: the total number of absences and the current streak of consecutive late days. Increment the absence counter whenever you see 'A'. For 'L', increment a running streak counter; reset it to zero when you encounter 'A' or 'P'. The moment the absence count reaches 2 or the late streak reaches 3, the record is invalid and you can return early. This approach works because the rules depend only on local counts, so a single linear iteration over the string captures everything needed.
The key insight is that you do not need additional data structures. Two integer counters are enough to enforce both constraints. Since each character is processed exactly once, the runtime is O(n), and the memory usage stays O(1). This is the standard interview solution because it is simple, efficient, and easy to reason about.
Approach 2: Regular Expression Matching (O(n) time, O(1) space)
The rules can also be expressed as a pattern check using regular expressions. A record becomes invalid if it matches either of these patterns: two absences anywhere (A.*A) or three consecutive lates (LLL). With regex support, you simply test whether the string matches A.*A|LLL. If it does, the attendance record violates the rules.
This approach is concise and expressive. It shifts the pattern detection to the regex engine instead of manual iteration. Under typical implementations, the scan still runs in O(n) time because the engine processes the string linearly. Space usage remains O(1) aside from minimal regex overhead. It is convenient in languages with strong regex support such as Python, Java, and JavaScript, though interviewers usually expect the manual scan.
Recommended for interviews: The single-pass counter approach is what interviewers expect. It demonstrates clear reasoning about constraints, efficient iteration over a string, and constant space usage. The regex version is elegant for quick scripting, but the explicit scan better shows algorithmic thinking and control over edge cases.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Single Pass Check | O(n) | O(1) | Best general solution. Preferred in coding interviews due to simplicity and constant memory. |
| Regular Expression Matching | O(n) | O(1) | Useful when regex support is strong and you want a concise pattern-based validation. |