Skip to content

Commit 2de95f1

Browse files
committed
feat : 인프런 3주차 36 올바른 괄호
1 parent 68175ef commit 2de95f1

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package sgyj.inflearn.yeji.week4;
2+
3+
import java.util.Scanner;
4+
import java.util.Stack;
5+
6+
public class Solution36 {
7+
8+
// 올바른 괄호
9+
public static String solution(String input){
10+
Stack<String> compare = new Stack<>();
11+
int i = 1;
12+
compare.push( String.valueOf( input.charAt( 0) ) );
13+
while(i < input.length()){
14+
String key = String.valueOf(input.charAt(i));
15+
if(!compare.isEmpty()){
16+
if(!compare.peek().equals(key) && key.equals( ")" )){
17+
compare.pop();
18+
}else{
19+
compare.push( key );
20+
}
21+
}else{
22+
compare.push(key);
23+
}
24+
i++;
25+
}
26+
return compare.isEmpty()?"YES":"NO";
27+
}
28+
29+
30+
public static void main(String[] args){
31+
Scanner sc = new Scanner(System.in);
32+
String input = sc.nextLine();
33+
34+
System.out.println(solution(input));
35+
}
36+
37+
}

0 commit comments

Comments
 (0)