Difficulty: Easy | Acceptance: 50.20% | Paid: No Topics: String
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.
- Examples
- Constraints
- Single Pass with Counters
- String Methods Approach
- Regular Expression Approach
Examples
Input: s = "PPALLP"
Output: true
Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.
Input: s = "PPALLL"
Output: false
Explanation: The student was late 3 consecutive days in the last 3 days.
Constraints
1 <= s.length <= 1000
s[i] is either 'A', 'L', or 'P'.
Single Pass with Counters
Intuition Iterate through the string once, tracking the total count of absences and the current streak of consecutive late days.
Steps
- Initialize counters for absences and consecutive late days
- For each character, update the appropriate counter
- Reset consecutive late counter when we see ‘A’ or ‘P’
- Return false early if either condition is violated
- Return true if we complete the iteration without violations
class Solution:
def checkRecord(self, s: str) -> bool:
count_A = 0
consecutive_L = 0
for char in s:
if char == 'A':
count_A += 1
consecutive_L = 0
if count_A >= 2:
return False
elif char == 'L':
consecutive_L += 1
if consecutive_L >= 3:
return False
else: # 'P'
consecutive_L = 0
return TrueComplexity
- Time: O(n) where n is the length of the string
- Space: O(1) only using constant extra space
- Notes: Early termination possible when conditions are violated
String Methods Approach
Intuition Use built-in string methods to directly check the two conditions: count of ‘A’ and presence of “LLL” substring.
Steps
- Count occurrences of ‘A’ using count method
- Check if “LLL” exists in the string using contains or includes
- Return true only if count of ‘A’ is less than 2 AND “LLL” is not present
class Solution:
def checkRecord(self, s: str) -> bool:
return s.count('A') < 2 and 'LLL' not in sComplexity
- Time: O(n) for counting and substring search
- Space: O(1) constant extra space
- Notes: More readable but may traverse string multiple times internally
Regular Expression Approach
Intuition Use regex pattern matching to check for invalid patterns: two or more ‘A’s or three consecutive ‘L’s.
Steps
- Create regex pattern to match two ‘A’s with any characters in between
- Create regex pattern to match three consecutive ‘L’s
- Return true if neither pattern is found in the string
import re
class Solution:
def checkRecord(self, s: str) -> bool:
return not (re.search(r'A.*A', s) or re.search(r'LLL', s))Complexity
- Time: O(n) regex matching complexity
- Space: O(1) constant extra space for patterns
- Notes: Elegant but regex overhead may be slower for simple patterns