
You are given a string s representing a student's attendance record, where each character is used to mark the day's attendance (absence, lateness, presence). The record contains only the following three characters:
'A': Absent, Absent
'L': Late, late
'P': Present, present
Students can receive attendance bonuses if they meet both of the following conditions:
(1). Student absences ('A') are strictly less than two days based on total attendance.
(2). Students will not have a record of being late ('L') for 3 or more consecutive days.
Returns true if the student is eligible for an attendance bonus; otherwise, returns false.
Input: s = "PPALLP" Output: true Explanation: The student has been absent less than 2 times, and there is no record of being late for 3 or more consecutive days.
Input: s = "PPALLL" Output: false Explanation: The student has been late for the last three consecutive days, so he is not eligible for the attendance bonus.
def checkRecord(s: str) -> bool:
if s.count('A') >= 2:
return False
if 'LLL' in s:
return False
return True
print(checkRecord('PPALLP'))
print(checkRecord('LPPALLP'))
print(checkRecord('PPLLLA'))
another solution
def checkRecord(s: str) -> bool:
absents = lates = 0 # 统计缺勤和迟到的次数,初始化为0
for c in s: # 统计缺勤的次数
if c == "A":
absents += 1
if absents >= 2: # 缺勤次数大于2
return False
if c == "L": # 统计迟到的次数
lates += 1
if lates >= 3: # 迟到次数大于3
return False
else: # 清空迟到次数
lates = 0
return True
