Skip to content

Commit 0adb8b8

Browse files
committed
Student attendance record I.
1 parent 1e2e5b0 commit 0adb8b8

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
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:
3+
4+
- 'A': Absent.
5+
- 'L': Late.
6+
- 'P': Present.
7+
8+
The student is eligible for an attendance award if they meet both of the following criteria:
9+
10+
- The student was absent ('A') for strictly fewer than 2 days total.
11+
- The student was never late ('L') for 3 or more consecutive days.
12+
13+
Return true if the student is eligible for an attendance award, or false otherwise.
14+
15+
 
16+
17+
Example 1:
18+
Input: s = "PPALLP"
19+
Output: true
20+
Explanation: The student has fewer than 2 absences and was never late 3 or more consecutive days.
21+
22+
Example 2:
23+
Input: s = "PPALLL"
24+
Output: false
25+
Explanation: The student was late 3 consecutive days in the last 3 days, so is not eligible for the award.
26+
 
27+
28+
Constraints:
29+
- 1 <= s.length <= 1000
30+
- s[i] is either 'A', 'L', or 'P'.
31+
*/
32+
class Solution {
33+
func checkRecord(_ s: String) -> Bool {
34+
if s.count == 1 { return true }
35+
var absentCount = 0
36+
var lateCount = 0
37+
for (i, c) in s.enumerated() {
38+
if absentCount == 2 || lateCount == 3 {
39+
return false
40+
}
41+
if c == "A" {
42+
absentCount += 1
43+
} else if c == "L" {
44+
if i == 0 {
45+
lateCount += 1
46+
} else {
47+
if s[s.index(s.startIndex, offsetBy: i - 1)] == "L" {
48+
lateCount += 1
49+
} else {
50+
lateCount = 1
51+
}
52+
}
53+
}
54+
}
55+
if absentCount == 2 || lateCount == 3 {
56+
return false
57+
}
58+
return true
59+
}
60+
}
61+
62+
let s = Solution()
63+
let r = s.checkRecord("LALL")
64+
print(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/551.Student Attendance Record I.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
104. [Detect Capital](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/520.Detect%20Captial.playground/Contents.swift)
109109
105. [Longest Uncommon Subsequence I](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/521.Longest%20Uncommon%20Subsequence%20I.playground/Contents.swift)
110110
106. [Reverse String II](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/541.Reverse%20String%20II.playground/Contents.swift)
111+
107. [Student Attendance Record I](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/551.Student%20Attendance%20Record%20I.playground/Contents.swift)
111112

112113
#### Medium
113114

0 commit comments

Comments
 (0)