Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 31cb635

Browse files
Add solution for Student Attendance Record I
1 parent 86c222f commit 31cb635

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Algorithm exercises from LeetCode implemented in Java v11.
55

66
### String
77
- Sorting Sentence | [Problem](https://leetcode.com/problems/sorting-the-sentence) | [Solution](src/solutions/SortingSentence.java)
8+
- Student Attendance Record I | [Problem](https://leetcode.com/problems/student-attendance-record-i) | [Solution](src/solutions/StudentAttendanceRecord.java)
89
- Reverse Words in a String III | [Problem](https://leetcode.com/problems/reverse-words-in-a-string-iii) | [Solution](src/solutions/ReverseWordsInStringIII.java)
910

1011
### Array
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package solutions;
2+
3+
// [Problem] https://leetcode.com/problems/student-attendance-record-i
4+
class StudentAttendanceRecord {
5+
// String scan
6+
// O(n) time, O(1) space
7+
public boolean checkRecord(String s) {
8+
int absentCount = 0;
9+
for (int i = 0; i < s.length(); i++) {
10+
if (s.charAt(i) == 'A') {
11+
absentCount++;
12+
if (absentCount >= 2) {
13+
return false;
14+
}
15+
} else if (i <= s.length() - 3 && s.charAt(i) == 'L' && s.charAt(i + 1) == 'L' && s.charAt(i + 2) == 'L') {
16+
return false;
17+
}
18+
}
19+
return true;
20+
}
21+
22+
// Test
23+
public static void main(String[] args) {
24+
StudentAttendanceRecord solution = new StudentAttendanceRecord();
25+
26+
String input1 = "PPALLP";
27+
boolean output1 = solution.checkRecord(input1);
28+
System.out.println("Test 1 passed? " + (output1 == true));
29+
30+
String input2 = "PPALLL";
31+
boolean output2 = solution.checkRecord(input2);
32+
System.out.println("Test 1 passed? " + (output2 == false));
33+
}
34+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /