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) {
int cnta=0, cntl=0;
boolean flag=false;
for(int i=0; i<s.length(); i++){
if(s.charAt(i)=='A') {
cnta++;
cntl = 0;
}else if(s.charAt(i)=='L'){
if(flag==false){
cntl++;
flag = true;
}else
cntl++;
if(cntl==3) break;
}else{
flag = false;
cntl = 0;
}
}
if(cnta>1 || cntl==3) return false;
return true;
}
}
啊啊啊,正则。。。
public boolean checkRecord(String s) {
return !s.matches(".*LLL.*|.*A.*A.*");
}