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 public boolean checkRecord(String s) {
3 int countA = 0, lenL = 0;
4 for (char c : s.toCharArray()) {
5 if (c == 'A') {
6 countA++;
7 if (countA >= 2) return false;
8 }
9 if (c == 'L') {
10 lenL++;
11 if (lenL >= 3) return false;
12 } else {
13 lenL = 0;
14 }
15 }
16 return true;
17 }
18}
The Java solution uses a for-each loop to iterate over the string's characters. We increment counts of 'A' and monitor consecutive 'L's, returning false once any condition is unsatisfied.
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.