551. 学生出勤记录 I
给定一个字符串来代表一个学生的出勤记录,这个记录仅包含以下三个字符:
'A' : Absent,缺勤
'L' : Late,迟到
'P' : Present,到场
如果一个学生的出勤记录中不超过一个'A'(缺勤)并且不超过两个连续的'L'(迟到),那么这个学生会被奖赏。
你需要根据这个学生的出勤记录判断他是否会被奖赏。
示例 1:
输入: "PPALLP"
输出: True
思路:
1、统计原串中A的个数,大于1则返回False;
2、遍历原串,当遇到L时(下标为index),截取index往后的三个字符,若是LLL则返回False;
3、遍历结束,返回True。
1 class Solution(object): 2 def checkRecord(self, s): 3 """ 4 :type s: str 5 :rtype: bool 6 """ 7 # 原串中A的个数大于1 8 if s.count('A') > 1: 9 return False 10 for index, ch in enumerate(s): 11 # 连续出现三个L 12 if ch == 'L' and s[index:index + 3] == 'LLL': 13 return False 14 return True 15 16 17 if __name__ == '__main__': 18 solution = Solution() 19 print(solution.checkRecord("PPALLP")) 20 print(solution.checkRecord("ALL"))