[LeetCode] Student Attendance Record I
文章目录
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;
}
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论