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:

1
2
Input: "PPALLP"
Output: True

Example 2:

1
2
Input: "PPALLL"
Output: False

分析


如果学生缺席次数大于等于两次,连续迟到的次数大于两次,则不能得出席奖。

解决方案1(Java)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public boolean checkRecord(String s) {
int aCount = 0, lCount = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'A') {
aCount++;
}
if (s.charAt(i) == 'L') {
lCount++;
} else {
lCount = 0;
}
if (aCount >= 2 || lCount > 2) {
return false;
}
}
return true;
}
}

相关问题


题目来源