[leetcode] problem 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

No.1

Input: “PPALLP”

Output: True

No.2

Input: “PPALLL”

Output: False

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public boolean (String s) {
int countA = 0;
int countL = 0;

for (char ch : s.toCharArray()) {
if (ch == 'A')
countA++;

if (ch == 'L')
countL++;
else
countL = 0;

if (countA > 1 || countL > 2)
return false;
}

return true;
}