-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.java
More file actions
71 lines (58 loc) Β· 1.75 KB
/
Main.java
File metadata and controls
71 lines (58 loc) Β· 1.75 KB
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
66
67
68
69
70
71
package _2020_KAKAO_BLIND_RECRUITMENT.P2;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Solution sol = new Solution();
System.out.println(sol.solution("(()())()"));
System.out.println(sol.solution(")("));
System.out.println(sol.solution("()))((()"));
}
}
class Solution {
static String u, v;
public String solution(String p) {
return make(p);
}
static String make(String p) {
if (p.equals("")) return p;
divide(p);
if (isValid(u)) {
return u + make(v);
} else {
String temp = u.substring(1, u.length() - 1);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < temp.length(); i++) {
if (temp.charAt(i) == ')') sb.append("(");
else sb.append(")");
}
return "(" + make(v) + ")" + sb.toString();
}
}
static void divide(String w) {
int left = 0, right = 0;
for (int i = 0; i < w.length(); i++) {
if (w.charAt(i) == '(') {
left ++;
} else if (w.charAt(i) == ')') {
right ++;
}
if (left == right) {
u = w.substring(0, i + 1);
v = w.substring(i + 1);
break;
}
}
}
static boolean isValid(String s) {
Stack<Character> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '(') {
stack.push(s.charAt(i));
} else if (s.charAt(i) == ')') {
if (stack.empty()) return false;
stack.pop();
}
}
return true;
}
}