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.
This approach involves a single traversal of the string, during which we track the count of 'A's and check for any sequence of 'L's greater than or equal to 3. If any of the conditions for disqualification is met, we terminate early.
The C solution uses a loop to iterate over the input string. We maintain a counter for 'A' and track consecutive 'L's. If we encounter two 'A's or three consecutive 'L's, we quickly return false. Otherwise, return true after loop completion.
Time Complexity: O(n) where n is the length of the string, as we traverse the string once.
Space Complexity: O(1) since we use a constant amount of space.
This approach uses pattern matching to detect invalid attendance records. We utilize regular expressions to verify that no segment of 3 'L's exists and that the count of 'A's is within the acceptable limit.
The Python solution leverages the built-in re module to perform regex operations. It counts 'A' occurrences and checks the presence of 'LLL' directly using regex.
Python
Java
JavaScript
Time Complexity: O(n) primarily due to the traversal to count 'A's and check for 'LLL'.
Space Complexity: O(1), though the re module might use additional space depending on implementation.
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Single Pass Check | Time Complexity: O(n) where n is the length of the string, as we traverse the string once. |
| Regular Expression Matching | Time Complexity: O(n) primarily due to the traversal to count 'A's and check for 'LLL'. |
| Default Approach | — |
| 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. |
Coding Interview Tutorial 132 - Student Attendance Record I [LeetCode] • Amell Peralta • 1,707 views views
Watch 9 more video solutions →Practice Student Attendance Record I with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor