Question:
You are given a string representing an attendance record for a student. The record only contains the following three characters:
‘A’ : Absent.
‘L’ : Late.
‘P’ : Present.
A student could be rewarded if his attendance record doesn’t contain more than one ‘A’ (absent) or more than two continuous ‘L’ (late).
You need to return whether the student could be rewarded according to his attendance record.
Example:
Input: "PPALLP"
Output: True
Input: "PPALLL"
Output: False
Solution:
/**
* @param {string} s
* @return {boolean}
*/
var checkRecord = function(s) {
const regx = /(A.*A)|L{3}/g;
return !regx.test(s);
};
Runtime: 68 ms, faster than 77.08% of JavaScript online submissions for Student Attendance Record I.
/**
* @param {string} s
* @return {boolean}
*/
var checkRecord = function(s) {
let a = 0;
for (let i in s) {
if(s[i] === 'A') a++;
if(s[i] === 'L' && s.length - i >= 3 ) {
if(s[i - 0 + 1] === 'L' && s[i - 0 + 2] === 'L') return false;
}
}
return a <= 1;
};
Runtime: 80 ms, faster than 14.58% of JavaScript online submissions for Student Attendance Record I.