Skip to content

Commit 9f9f91b

Browse files
committed
Add a solution to StudentAttendanceRecordI
1 parent f3c744b commit 9f9f91b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Question Link: https://leetcode.com/problems/student-attendance-record-i/
3+
* Primary idea: Iterate the string and check late & absent counts to check the eligibility.
4+
*
5+
* Time Complexity: O(n), Space Complexity: O(1)
6+
*
7+
*/
8+
9+
class StudentAttendanceRecordI {
10+
func checkRecord(_ s: String) -> Bool {
11+
var ACount = 0, LCount = 0
12+
let sChars = Array(s)
13+
14+
for i in 0..<s.count {
15+
if sChars[i] == "A" {
16+
ACount += 1
17+
18+
if ACount >= 2 {
19+
return false
20+
}
21+
} else if sChars[i] == "L" {
22+
if i > 0 && sChars[i - 1] == "L" {
23+
LCount += 1
24+
25+
if LCount >= 3 {
26+
return false
27+
}
28+
} else {
29+
LCount = 1
30+
}
31+
}
32+
}
33+
34+
return true
35+
}
36+
}

0 commit comments

Comments
 (0)