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.
1var checkRecord = function(s) {
2 let countA = 0, lenL = 0;
3 for (let c of s) {
4 if (c === 'A') {
5 countA++;
6 if (countA >= 2) return false;
7 }
8 if (c === 'L') {
9 lenL++;
10 if (lenL >= 3) return false;
11 } else {
12 lenL = 0;
13 }
14 }
15 return true;
16};
The JavaScript solution processes the string using a for-of loop. It counts 'A' and maintains the length of current consecutive 'L's, returning false if the student would be ineligible for the award based on criteria.
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.
1var checkRecord = function(s)
The JavaScript version uses built-in methods such as split, filter, and includes to check if the award criteria are violated. By converting the string to an array and filtering, we can determine 'A' count and check "LLL" presence.