Similar Problems
Similar Problems not available
Student Attendance Record I - Leetcode Solution
Companies:
LeetCode: Student Attendance Record I Leetcode Solution
Difficulty: Easy
Topics: string
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 true if the student could be rewarded according to his attendance record, and false otherwise.
Solution:
We can solve this problem by iterating over the characters of the given string and checking the conditions mentioned in the problem statement.
Step 1: Initialize absent count (abs_count) and late count (late_count) to 0.
Step 2: Iterate over the characters of the string, and for each character, do the following:
- If the character is 'A', increment the abs_count by 1.
- If the character is 'L', increment the late_count by 1. If late_count becomes greater than 2, then the student cannot be rewarded, so return false.
- If the character is 'P', reset the late_count to 0.
Step 3: After iterating over the string, check if abs_count is less than or equal to 1 (i.e., the student has been absent less than twice). If not, return false.
Step 4: If the function has not yet returned false, then the student can be rewarded, so return true.
Code:
The code in Python for the above solution is as follows:
class Solution: def checkRecord(self, s: str) -> bool: abs_count = 0 # Initialize the absent count to 0 late_count = 0 # Initialize the late count to 0 for c in s: if c == 'A': # If the student is absent, increment the absent count abs_count += 1 if abs_count > 1: # If the student has been absent more than once, return false return False elif c == 'L': # If the student is late, increment the late count late_count += 1 if late_count > 2: # If the student has been late more than twice in a row, return false return False else: # If the student is present, reset the late count late_count = 0 return True # If the function has not yet returned false, then the student can be rewarded.
Student Attendance Record I Solution Code
1