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'.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.
C++
Java
Python
C#
JavaScript
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.
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.
| 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'. |
Student Attendance Record II - Leetcode 552 - Python • NeetCodeIO • 10,077 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