1. Description
You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters :
- "A" : Absent
- "L" : Late
- "P" : Present
The student is eligible for an attendance award if they meet both of the following criteria :
- The student was absent('A') for stritly fewer than 2 days total.
- The student was never late('L') for 3 or more consecutive days.
Return True if the studnet is elgible for an attendance award, or False otherwise.
constraints :
- 1 <= s.length <= 1000
- s[i] is either 'A', 'L', or 'P'.
2. Algorithms
The two criterias that the studnet is eligible for an attendance award are same as below :
- The total count of absent("A") is fewer than 2.
- The consecutive days of late("L") is fewer than 3.
So when we iterate through the input string 'checkRecord', we need to return False when we encounter error case.
- Return False if count('A') >= 3.
- Return False if s[i : i+3] == 'LLL'.
- Start
- Declare variable named count initialized with 0.
- Repeat the steps while loop iterate through integers 0 - len(s) - 1.
- If s[i] == 'A', Increment count by 1.
- If count > 2, return False
- If i < len(s) - 3 and s[i : i+3] == 'LLL', return False
- If s[i] == 'A', Increment count by 1.
- Return True when the loop ends.
- End
3. Codes
class Solution:
def checkRecord(self, s: str) -> bool:
count = 0
for i in range(len(s)) :
if s[i] == 'A' :
count += 1
if count > 1 :
return False
if (i < len(s) - 2) and (s[i:i + 3] == 'LLL') :
return False
return True
4. Conclusion
'LeetCode > Easy' 카테고리의 다른 글
559 Maximum Depth of N-ary Tree (0) | 2022.09.06 |
---|---|
557 Reverse Words in a String III (0) | 2022.09.05 |
543 Diameter of Binary Tree (0) | 2022.09.05 |
541 Reverse String II (0) | 2022.09.05 |
530 Minimum Absolute Difference in BST (0) | 2022.09.05 |