-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
38 lines (34 loc) · 863 Bytes
/
Solution.java
File metadata and controls
38 lines (34 loc) · 863 Bytes
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
34
35
36
37
38
package programmers.lv2._002_ValidParentheses;
/*
문제: 올바른 괄호
레벨: Lv2
출처: Programmers
링크: https://school.programmers.co.kr/learn/courses/30/lessons/12909
*/
import java.util.Stack;
class Solution {
boolean solution(String s) {
// ( --> push
// ) --> pop
Stack<Character> stack = new Stack<>();
for (char c : s.toCharArray()) {
if (c == '(') {
stack.push(c);
} else {
if (stack.isEmpty()) {
// 닫는 괄호가 더 많은 경우
return false;
}
stack.pop();
}
}
return stack.isEmpty();
}
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.solution("()()")); // true
System.out.println(sol.solution("(())()")); // true
System.out.println(sol.solution("(()")); // false
System.out.println(sol.solution("())(")); // false
}
}