【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:

'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).

class Solution(object):

    def checkRecord(self, s):

        """

        :type s: str

        :rtype: bool

        """

        if not s: return None

        c = collections.Counter(s)

        if c['A']>1: return False

        count = 0

        for i in range(2, len(s)):

            if s[i]==s[i-1]==s[i-2]=='L':

                return False

        return True

1 分别判断两种情况,一种是A的情况,一种是L的情况

2 对于A,可用collections.Counter(s)得出每种情况的个数,如果A大于1,则返回Fasle

3  if c['A']>1: return False  注意c['A']要用中括号,而且里面字母用‘A’

4 对于L,则判断有没有连续三个L

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容