Similar Problems

Similar Problems not available

Student Attendance Record Ii - Leetcode Solution

Companies:

LeetCode:  Student Attendance Record Ii Leetcode Solution

Difficulty: Hard

Topics: dynamic-programming  

Problem Statement:

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 1:

Input: "PPALLP" Output: True

Explanation: The student has fewer than two absences and fewer than three continuous late records.

Example 2:

Input: "PPALLL" Output: False

Explanation: The student has more than two continuous late records.

Approach:

We need to check if the attendance record follows the given rules for attendance records of the student. We can check the attendance record by iterating through each character of the attendance record one by one to check for "A" and "LLL" pattern.

To check for "A" pattern we need to check if the attendance record contains less than or equal to one "A" character.

To check for "LLL" pattern we need to check if the attendance record contains three consecutive "L" characters.

In both cases, if we find that the attendance record is voilating the given rules for attendance records, we will return false.

Solution:

Here is the Python solution for the given problem statement:

def checkRecord(self, s: str) -> bool:

#check if the attendance record contains less than or equal to one "A" character if s.count('A') > 1: return False

#check if the attendance record contains three consecutive "L" characters if 'LLL' in s: return False

return True

Time Complexity: O(n)

Space Complexity: O(1)

In the above code, we have used the count() method to count the occurrence of "A" character in the attendance record string. We have used the in keyword to check if the string "LLL" is present in the attendance record.

We have checked for both patterns of "A" and "LLL" in the attendance record and returned the boolean value accordingly.

Student Attendance Record Ii Solution Code

1