Problem Description
Given a string s representing a student's attendance record, determine if the student is eligible for an award. Eligibility requires that the student has been absent strictly fewer than 2 times (i.e., <2 'A's) and has never been late for 3 or more consecutive days (i.e., no occurrence of "LLL"). Return true if the student meets both criteria, or false otherwise.
Key Insights
- Count the number of 'A' (Absent) characters in the string. The count must be less than 2.
- Check for consecutive occurrences of 'L' (Late) characters. There should not be any sequence of 3 or more consecutive 'L's.
- A single traversal of the string is sufficient to verify both conditions.
Space and Time Complexity
Time Complexity: O(n), where n is the length of the string. Space Complexity: O(1), using just a few counters regardless of the input size.
Solution
The solution involves iterating through the input string exactly once. During the iteration, two pieces of information are tracked:
- A counter for the total number of absences ('A').
- A counter for consecutive tardies ('L').
For each character:
- If it is an 'A', increment the absence counter. If the counter reaches 2, we can immediately return false.
- If it is an 'L', increment the consecutive late counter. If the consecutive counter reaches 3, return false immediately.
- If the character is 'P' (Present) or any character other than 'L', reset the consecutive late counter to 0.
If the loop completes without triggering either disqualification condition, return true.