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
Solution1: Simulation
Time complexity: O(n)
Space complexity: O(1)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
// Author: Huahua // Running time: 3 ms class Solution { public: bool checkRecord(string s) { int a{0}; int l{0}; for(char c: s) { if (c == 'A') ++a; if (c == 'L') ++l; else l=0; if (a > 1 || l > 2) return false; } return true; } }; |
Solution 2: Regex
1 2 3 4 5 6 7 8 |
// Author: Huahua // Running time: 6 ms class Solution { public: bool checkRecord(string s) { return !std::regex_search(s, std::regex("A.*A|LLL")); } }; |
请尊重作者的劳动成果,转载请注明出处!花花保留对文章/视频的所有权利。
如果您喜欢这篇文章/视频,欢迎您捐赠花花。
If you like my articles / videos, donations are welcome.
Be First to Comment