Repository navigation
Expand file tree
/
Copy pathRemoveOuterParenthesis.java
More file actions
66 lines (51 loc) · 1.75 KB
/
Copy pathRemoveOuterParenthesis.java
File metadata and controls
66 lines (51 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
package com.abn.dsalgos.datastructure.stack;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
/*
Given a valid parentheses string s, consider its primitive decomposition: s = P1 + P2 + ... + Pk,
where Pi are primitive valid parentheses strings.
Return s after removing the outermost parentheses of every primitive string in the primitive decomposition of s.
Input: s = "(()())(())"
Output: "()()()"
Explanation:
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
Input: s = "(()())(())(()(()))"
Output: "()()()()(())"
Input: s = "()()"
Output: ""
Explanation:
*/
public class RemoveOuterParenthesis {
public String removeOuterParentheses(String s) {
Stack<Character> stack = new Stack<>();
List<String> list = new LinkedList<>();
int start = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) == '(') {
if(stack.isEmpty()) {
start = i;
}
stack.push(s.charAt(i));
} else {
if(!stack.isEmpty()) {
stack.pop();
}
if(stack.isEmpty()) {
list.add(s.substring(start, i+1));
}
}
}
StringBuilder sb = new StringBuilder();
for(String input : list) {
sb.append(input, 1, input.length()-1);
}
return sb.toString();
}
public static void main(String[] args) {
String s = "(()())(())";
RemoveOuterParenthesis parenthesis = new RemoveOuterParenthesis();
System.out.println(parenthesis.removeOuterParentheses(s));
}
}