Skip to content

Commit d8ab7f7

Browse files
committed
update 1021.remove-outermost-parentheses.java
1 parent a7e1142 commit d8ab7f7

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* @lc app=leetcode id=1021 lang=java
3+
*
4+
* [1021] Remove Outermost Parentheses
5+
*
6+
* https://leetcode.com/problems/remove-outermost-parentheses/description/
7+
*
8+
* algorithms
9+
* Easy (78.71%)
10+
* Total Accepted: 118.2K
11+
* Total Submissions: 150.1K
12+
* Testcase Example: '"(()())(())"'
13+
*
14+
* A valid parentheses string is either empty (""), "(" + A + ")", or A + B,
15+
* where A and B are valid parentheses strings, and + represents string
16+
* concatenation.  For example, "", "()", "(())()", and "(()(()))" are all
17+
* valid parentheses strings.
18+
*
19+
* A valid parentheses string S is primitive if it is nonempty, and there does
20+
* not exist a way to split it into S = A+B, with A and B nonempty valid
21+
* parentheses strings.
22+
*
23+
* Given a valid parentheses string S, consider its primitive decomposition: S
24+
* = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.
25+
*
26+
* Return S after removing the outermost parentheses of every primitive string
27+
* in the primitive decomposition of S.
28+
*
29+
*
30+
*
31+
* Example 1:
32+
*
33+
*
34+
* Input: "(()())(())"
35+
* Output: "()()()"
36+
* Explanation:
37+
* The input string is "(()())(())", with primitive decomposition "(()())" +
38+
* "(())".
39+
* After removing outer parentheses of each part, this is "()()" + "()" =
40+
* "()()()".
41+
*
42+
*
43+
*
44+
* Example 2:
45+
*
46+
*
47+
* Input: "(()())(())(()(()))"
48+
* Output: "()()()()(())"
49+
* Explanation:
50+
* The input string is "(()())(())(()(()))", with primitive decomposition
51+
* "(()())" + "(())" + "(()(()))".
52+
* After removing outer parentheses of each part, this is "()()" + "()" +
53+
* "()(())" = "()()()()(())".
54+
*
55+
*
56+
*
57+
* Example 3:
58+
*
59+
*
60+
* Input: "()()"
61+
* Output: ""
62+
* Explanation:
63+
* The input string is "()()", with primitive decomposition "()" + "()".
64+
* After removing outer parentheses of each part, this is "" + "" = "".
65+
*
66+
*
67+
*
68+
*
69+
*
70+
*
71+
* Note:
72+
*
73+
*
74+
* S.length <= 10000
75+
* S[i] is "(" or ")"
76+
* S is a valid parentheses string
77+
*
78+
*
79+
*
80+
*
81+
*
82+
*
83+
*
84+
*/
85+
class Solution {
86+
public String removeOuterParentheses(String S) {
87+
StringBuilder sb = new StringBuilder();
88+
// Keep track of opening parentheses
89+
int open = 0;
90+
// String buffer
91+
// Loop over string (in char[] for convenience and perf)
92+
for (char c : S.toCharArray()) {
93+
// Detecting an opening '(' will increment open, while detecting closing ')'
94+
// will decrement open.
95+
// This way it can keep appending chars into the buf if open > 0 or while
96+
// the very first opening hasn't found the closing.
97+
// Very similar to a Stack data structure. Where open is the number of
98+
// stacked openings, then it pops off from the stack whenever it finds
99+
// closing.
100+
if (c == '(') {
101+
open++;
102+
if (open == 1)
103+
continue;
104+
} else if (c == ')') {
105+
open--;
106+
}
107+
108+
// If open > 0, Meaning theres, an open outer parenthesis:
109+
// Append into buf
110+
if (open > 0) {
111+
sb.append(c);
112+
}
113+
}
114+
115+
return sb.toString();
116+
}
117+
}

0 commit comments

Comments
 (0)