leetcode 551. Student Attendance Record I

You are given a string representing an attendance record for a student. The record only contains the following three characters:

  1. 'A' : Absent.
  2. 'L' : Late.
  3. '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
class Solution(object):
    def checkRecord(self, s):
        """
        :type s: str
        :rtype: bool
        """
        return not(s.count('A') > 1 or s.count('LLL') > 0)

使用计数器:

class Solution(object):
    def checkRecord(self, s):
        """
        :type s: str
        :rtype: bool
        """
        a = l = 0
        for c in s:
            if c == 'A':
                a += 1
                l = 0
            elif c == 'L':
                l += 1
            else:
                l = 0
            if a > 1 or l >= 3:
                return False
        return True
        

注意黄色部分代码,重置计数器!

原文地址:https://www.cnblogs.com/bonelee/p/8727447.html