-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveOutermostParentheses.java
38 lines (35 loc) · 1.03 KB
/
RemoveOutermostParentheses.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
34
35
36
37
38
package com.company;
public class RemoveOutermostParentheses {
public static void main(String[] args) throws Exception {
String s = "(()())(())";
System.out.println(removeOuterParentheses(s));
}
public static String removeOuterParentheses(String S) {
char[] pchar = S.toCharArray();
int start = 0;
int end = 0;
boolean isStart = false;
StringBuilder result = new StringBuilder();
for (int i = 0; i < pchar.length; i++) {
if (pchar[i] == '(') {
++start;
isStart = true;
} else if (pchar[i] == ')') {
++end;
isStart = false;
}
if (start == end) {
start = 0;
end = 0;
}
if (start > 1) {
if (isStart) {
result.append('(');
} else {
result.append(')');
}
}
}
return result.toString();
}
}