Sponsored
Sponsored
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.
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.
1class Solution:
2 def checkRecord(self, s: str) -> bool:
3 countA = 0
4 lenL = 0
5 for c in s:
6 if c == 'A':
7 countA += 1
8 if countA >= 2:
9 return False
10 if c == 'L':
11 lenL += 1
12 if lenL >= 3:
13 return False
14 else:
15 lenL = 0
16 return True
The Python solution iterates over the string using a for-loop. We maintain and evaluate a counter for 'A' and the length of consecutive 'L's. Any violation of conditions results in an early false return.
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.
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.
1import re
2class Solution:
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.