Question:

You are given a string representing an attendance record for a student. The record only contains the following three characters:

  1. ‘A’ : Absent.

  2. ‘L’ : Late.

  3. ‘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:

  1. Input: "PPALLP"
  2. Output: True
  1. Input: "PPALLL"
  2. Output: False

Solution:

  1. /**
  2. * @param {string} s
  3. * @return {boolean}
  4. */
  5. var checkRecord = function(s) {
  6. const regx = /(A.*A)|L{3}/g;
  7. return !regx.test(s);
  8. };

Runtime: 68 ms, faster than 77.08% of JavaScript online submissions for Student Attendance Record I.

  1. /**
  2. * @param {string} s
  3. * @return {boolean}
  4. */
  5. var checkRecord = function(s) {
  6. let a = 0;
  7. for (let i in s) {
  8. if(s[i] === 'A') a++;
  9. if(s[i] === 'L' && s.length - i >= 3 ) {
  10. if(s[i - 0 + 1] === 'L' && s[i - 0 + 2] === 'L') return false;
  11. }
  12. }
  13. return a <= 1;
  14. };

Runtime: 80 ms, faster than 14.58% of JavaScript online submissions for Student Attendance Record I.