551. Student Attendance Record I 学生出勤记录 I

@TOC

# 题目描述

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
``````

# 解题方法

# 正则表达式

``````public class Solution {
public boolean checkRecord(String s) {
return !s.matches(".*A.*A.*") && !s.matches(".*LLL.*");
}
}
``````

python写法如下，注意的是需要把正则表达式写在第一个参数，字符串s作为第二个参数。

``````class Solution:
def checkRecord(self, s):
"""
:type s: str
:rtype: bool
"""
return not re.match(".*A.*A.*", s) and not re.match(".*LLL.*", s)
``````

# 统计

``````class Solution:
def checkRecord(self, s):
"""
:type s: str
:rtype: bool
"""
N = len(s)
s = s + "P"
countA = 0
countL = 0
for i in range(N):
if s[i] == "A":
countA += 1
if countA > 1:
return False
if s[i] == "L":
countL += 1
if countL >= 3:
return False
else:
countL = 0
return True
``````

# 日期

2017 年 4 月 21 日 2018 年 11 月 17 日 —— 美妙的周末，美丽的天气