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.
1#include <string>
2
3class Solution {
4public:
5 bool checkRecord(std::string s) {
6 int countA = 0;
7 int lenL = 0;
8 for (char c : s) {
9 if (c == 'A') {
10 countA++;
11 if (countA >= 2) return false;
12 }
13 if (c == 'L') {
14 lenL++;
15 if (lenL >= 3) return false;
16 } else {
17 lenL = 0;
18 }
19 }
20 return true;
21 }
22};
The C++ implementation uses a range-based for-loop to process the string. Similar to the C solution, we keep counters for 'A' and consecutive 'L's, and return false if conditions are violated.
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.