tag: PPA int resolves the problem of emptying a matching regular expression loop string
Leetcode: Attendance record of Students | "551" topic description
Given a string to represent a student's attendance record, this record contains only the following three characters:
- ' A ': Absent, absence
- ' L ': late, late
- ' P ': Present, on the scene
If a student's attendance record does not exceed a ' a ' (absence) and does not exceed two consecutive ' L ' (late), then the student will be rewarded.
You need to judge whether he will be rewarded according to the student's attendance record.
Example 1:
Input: "PPALLP" output: True
Example 2:
Input: "ppalll" output: False
Problem analysis
For string problems, we need to reflect on the so-called rock-regular expression .
Many problems can be solved quickly, because we are suffering too little, so it backfired.
l{3,} can represent 3 consecutive and above L appear, a.*a can represent 2 times a , so if the regular match is successful, then it will not be rewarded!
Java
Class Solution {public Boolean Checkrecord (String s) { if (s.matches (". *l{3,}.*")) return false; if (S.matches (". *a.*a.*")) return false; return true; }}
But the above for This problem, the Regular expression solution is concise but not efficient .
We loop through the string once, a variable counts the number of times a, a variable counts the number of consecutive L times, there is a technique for successive times, when the first L is encountered, the next is not L, and the count variable is emptied immediately .
public Boolean Checkrecord (String s) { int a = 0; int l = 0; for (char C:s.tochararray ()) { if (c== ' A ') a++; if (c== ' L ') l++; else l=0; if (a>1| | l>=3) return false; } return true; }
Leetcode: Attendance record for students | "551"