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 <stdbool.h>
2#include <string.h>
3
4bool checkRecord(char * s) {
5 int countA = 0;
6 int lenL = 0;
7 for (int i = 0; s[i] != '\0'; ++i) {
8 if (s[i] == 'A') {
9 countA++;
10 if (countA >= 2) return false;
11 }
12 if (s[i] == 'L') {
13 lenL++;
14 if (lenL >= 3) return false;
15 } else {
16 lenL = 0;
17 }
18 }
19 return true;
20}
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.
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 java.util.regex.*;
In the Java solution, regex is employed to count occurrences of 'A'. The presence of "LLL" is checked using contains method, ensuring not more than one result from 'A' search allows eligibility.