leetcode-551. 学生出勤纪录 I

一、问题描述


给定一个字符串来代表一个学生的出勤纪录,这个纪录仅包含以下三个字符:

  1. 'A' : Absent,缺勤
  2. 'L' : Late,迟到
  3. 'P' : Present,到场

如果一个学生的出勤纪录中不超过一个'A'(缺勤)并且不超过两个连续的'L'(迟到),那么这个学生会被奖赏。

你需要根据这个学生的出勤纪录判断他是否会被奖赏。

示例 1:

输入: "PPALLP"
输出: True

示例 2:

输入: "PPALLL"
输出: False

二、代码和思路

1.首先构建两个变量count_A,count_L分别记录A和连续L的个数

2.变量s,发现一个A时count_A就加1,如果count_A大于1则立即return False,碰到L时count_L就加1,如果s[i]非L时则count_L置0,如果count_L大于2就理解return False

3.没出现return False的情况则最后return True

class Solution(object):
    def checkRecord(self, s):
        """
        :type s: str
        :rtype: bool
        """
        n=len(s)
        count_A,count_L=0,0
        for i in range(n):
            if s[i]=='A':
                count_A += 1
                if count_A>1:
                    return False
            if s[i]=='L':
                count_L += 1
                if count_L>2:
                    return False
            if s[i] != 'L':
                count_L=0
        return True

三、运行结果