-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
33 lines (28 loc) · 849 Bytes
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
// Stack(Brackets)
// https://app.codility.com/programmers/lessons/7-stacks_and_queues/brackets/
// Created by hyungwook on 2021/10/29.
//
import java.util.Stack;
public class Solution {
public int solution(String S) {
char[] array = S.toCharArray();
Stack<Character> stack = new Stack<>();
for (char c : array) {
if (c == ')' && !stack.isEmpty() && stack.peek() == '(') {
stack.pop();
} else if (c == ']' && !stack.isEmpty() && stack.peek() == '[') {
stack.pop();
} else if (c == '}' && !stack.isEmpty() && stack.peek() == '{') {
stack.pop();
} else {
stack.push(c);
}
}
if (stack.isEmpty()) {
return 1;
} else {
return 0;
}
}
}