-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path211230_1.txt
65 lines (65 loc) · 1.72 KB
/
211230_1.txt
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//카카오 괄호변환
import java.util.Stack;
class Solution {
public Boolean isRight(String str){
Stack<String> stack = new Stack<>();
try{
for(int i=0;i<str.length();i++){
if(str.charAt(i) == '(')
stack.push("(");
if(str.charAt(i) == ')')
stack.pop();
}}catch(Exception e){
return false;
}
return true;
}
public String balanced(String str){
int num = 0;
String string = "";
for(int i=0;i<str.length();i++){
if(str.charAt(i) == '('){
num++;
string += "(";
}
if(str.charAt(i) == ')'){
num--;
string += ")";
}
if(num == 0){
return string;
}
}
return string;
}
public String solution(String p) {
String answer = "";
if(p.equals(""))
return p;
String u = balanced(p);
String v = p.substring(u.length(),p.length());
String str1 = "";
String str2 = "";
String str3 = "";
if(isRight(u)){
str1 = solution(v);
u += str1;
answer = u;
}
else{
str2 = "(";
str1 = solution(v);
str2 += str1;
str2 += ")";
str3 = u.substring(1,u.length()-1);
for(int i = 0;i<str3.length();i++){
if(str3.charAt(i) == '(')
str2 += ")";
if(str3.charAt(i) == ')')
str2 += "(";
}
answer = str2;
}
return answer;
}
}