Skip to content

Commit 0eef9de

Browse files
committed
solve problem Student Attendance Record I
1 parent 645c290 commit 0eef9de

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ All solutions will be accepted!
8989
|696|[Count Binary Substrings](https://leetcode-cn.com/problems/count-binary-substrings/description/)|[java/py/js](./algorithms/CountBinarySubstrings)|Easy|
9090
|121|[Best Time To Buy And Sell Stock](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/description/)|[java/py/js](./algorithms/BestTimeToBuyAndSellStock)|Easy|
9191
|599|[Minimum Index Sum Of Two Lists](https://leetcode-cn.com/problems/minimum-index-sum-of-two-lists/description/)|[java/py/js](./algorithms/MinimumIndexSumOfTwoLists)|Easy|
92+
|551|[Student Attendance Record I](https://leetcode-cn.com/problems/student-attendance-record-i/description/)|[java/py/js](./algorithms/StudentAttendanceRecordI)|Easy|
9293

9394
# Database
9495
|#|Title|Solution|Difficulty|
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Student Attendance Record I
2+
This problem is easy to solve
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public boolean checkRecord(String s) {
3+
List<Character> aStack = new ArrayList<Character>();
4+
List<Character> lStack = new ArrayList<Character>();
5+
Character lastCharacter = '0';
6+
7+
for (char c : s.toCharArray()) {
8+
if (c == 'A') {
9+
aStack.add(c);
10+
} else if (c == 'L') {
11+
if (lastCharacter != c) {
12+
lStack.clear();
13+
}
14+
lStack.add(c);
15+
}
16+
17+
if (aStack.size() > 1 || lStack.size() > 2) {
18+
return false;
19+
}
20+
21+
lastCharacter = c;
22+
}
23+
24+
return true;
25+
}
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var checkRecord = function(s) {
6+
let aStack = [],
7+
lStack = [],
8+
lastCharacter = null
9+
10+
for (let i = 0; i < s.length; i++) {
11+
let c = s[i]
12+
13+
if (c === 'A') {
14+
aStack.push(c)
15+
} else if (c === 'L') {
16+
if (lastCharacter !== c) {
17+
lStack = [c]
18+
} else {
19+
lStack.push(c)
20+
}
21+
}
22+
23+
if (aStack.length > 1 || lStack.length > 2) {
24+
return false
25+
}
26+
27+
lastCharacter = c
28+
}
29+
30+
return true
31+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution(object):
2+
def checkRecord(self, s):
3+
"""
4+
:type s: str
5+
:rtype: bool
6+
"""
7+
a_stack = []
8+
l_stack = []
9+
last_character = None
10+
11+
for c in s:
12+
if c == 'A':
13+
a_stack.append(c)
14+
elif c == 'L':
15+
if last_character != 'L':
16+
l_stack = [c]
17+
else:
18+
l_stack.append(c)
19+
20+
if len(a_stack) > 1 or len(l_stack) > 2:
21+
return False
22+
last_character = c
23+
24+
return True

0 commit comments

Comments
 (0)