Problem
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
Solution
class Solution {
public boolean checkRecord(String s) {
if (s == null || s.length() == 0) return true;
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (!map.containsKey(ch)) map.put(ch, 1);
else map.put(ch, map.get(ch)+1);
if (ch == 'A' && map.get(ch) > 1) return false;
if (i+3 <= s.length() && s.substring(i, i+3).equals("LLL")) {
return false;
}
}
return true;
}
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。