Title: Positions of Large Groups Source: leetcode.com
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
Example 2:
Input: “PPALLL”
Output: False
Example 3:
Input: “PPALLPLL”
Output: True
Python solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
''' https://leetcode.com/problems/student-attendance-record-i/ ''' class Solution(object): def checkRecord(self, s): """ :type s: str :rtype: bool """ aCount = 0 #prevC = '' lateCount = 0 reward = True for c in s: if c == 'A': aCount += 1 lateCount = 0 if aCount > 1: reward = False break if c == 'L': lateCount += 1 if lateCount > 2: reward = False break else: lateCount = 0 return reward |