Student Attendance Record I

Patrick Leaton
Problem Description

You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters:

  • 'A': Absent.
  • 'L': Late.
  • 'P': Present.

The student is eligible for an attendance award if they meet both of the following criteria:

  • The student was absent ('A') for strictly fewer than 2 days total.
  • The student was never late ('L') for 3 or more consecutive days.

Return true if the student is eligible for an attendance award, or false otherwise.

 

Example 1:

Input: s = "PPALLP"
Output: true
Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.

Example 2:

Input: s = "PPALLL"
Output: false
Explanation: The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.

 

Constraints:

  • 1 <= s.length <= 1000
  • s[i] is either 'A''L', or 'P'.

Problem Solution

#O(N) Time, O(1) Space
class Solution:
    def checkRecord(self, s: str) -> bool:
        a_count, l_count = 0, 0
        for i in range(len(s)):
            if s[i] == "A":
                a_count += 1
            if s[i] == "L":
                l_count += 1
            else:
                l_count = 0
            if a_count == 2 or l_count == 3:
                return False
        return True

Problem Explanation


This problem boils down to knowing how many global absences we have, and if we have a substring of "LLL".  

To check for this, all we need to do is count each absence we see, and also have a running count of each late we see which we will reset to zero whenever the current character is anything but an "L".  By doing this, we will just need to check within each iteration if we have an absent count of two or a late count of three, returning false if that is the case.

Otherwise, we will return true that the student is eligible for an attendance reward if we never found an absent count of two or a late count of three.


Let's start by initializing our absence counter and late counter to zero.

        a_count, l_count = 0, 0
 

Then, we will begin traversing the string.

        for i in range(len(s)):
 

Within each iteration, we will increment the current counter accordingly. 

If the student has an absence, we will increment the global absence counter by one.

            if s[i] == "A":
                a_count += 1

 

If the student has a late mark, we will increment the running late counter by one.

            if s[i] == "L":
                l_count += 1

 

However, if the student had anything other than a late mark, then we can reset the late counter to zero since we are worried about consecutive late marks.

            else:
                l_count = 0

 

If at any point in the iteration, we find that the student had two absences or three late marks, we will return false.

            if a_count == 2 or l_count == 3:
                return False

 

If we traversed the string and didn't return false, this student is eligible for an attendance reward.

        return True