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.
1public class Solution {
2 public bool CheckRecord(string s) {
3 int countA = 0, lenL = 0;
4 foreach (char c in s) {
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 C# solution uses a foreach loop to iterate through characters of the string. Like other language solutions, it employs counters for 'A's and consecutive 'L's, ensuring no conditions are breached.
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.