Skip to content

Commit ded538a

Browse files
committed
1021. Remove Outermost Parentheses
Signed-off-by: Zhaowei Fang <[email protected]>
1 parent 6d27850 commit ded538a

File tree

4 files changed

+100
-1
lines changed

4 files changed

+100
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ continually updating 😃.
6363
* [345. Reverse Vowels of a String](src/0345_reverse_vowels_of_a_string/reverse_vowels.go)&nbsp;&nbsp;&nbsp;*`string;`*&nbsp;&nbsp;*`double index`*
6464
* [438. Find All Anagrams in a String](src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go)&nbsp;&nbsp;&nbsp;*`sliding window`*
6565
* [557. Reverse Words in a String III](src/0557_reverse_words_in_a_string_3/reverse_words_in_a_string_3.go)
66+
* [1021. Remove Outermost Parentheses](src/1021_Remove_Outermost_Parentheses/remove_outmost_parentheses.go)&nbsp;&nbsp;&nbsp;*`stack`*
6667

6768
### Linked List
6869
* [2. Add Two Numbers](src/0002_add_two_numbers/add_two_numbers.go)&nbsp;&nbsp;&nbsp;*`recursion;`*&nbsp;&nbsp;*`math`*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
1021. Remove Outermost Parentheses
3+
4+
A valid parentheses string is either empty (""), "(" + A + ")", or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, "", "()", "(())()", and "(()(()))" are all valid parentheses strings.
5+
6+
A valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings.
7+
8+
Given a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.
9+
10+
Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.
11+
12+
13+
14+
Example 1:
15+
16+
Input: "(()())(())"
17+
Output: "()()()"
18+
Explanation:
19+
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
20+
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
21+
Example 2:
22+
23+
Input: "(()())(())(()(()))"
24+
Output: "()()()()(())"
25+
Explanation:
26+
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
27+
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
28+
Example 3:
29+
30+
Input: "()()"
31+
Output: ""
32+
Explanation:
33+
The input string is "()()", with primitive decomposition "()" + "()".
34+
After removing outer parentheses of each part, this is "" + "" = "".
35+
36+
37+
Note:
38+
39+
S.length <= 10000
40+
S[i] is "(" or ")"
41+
S is a valid parentheses string
42+
43+
44+
45+
*/
46+
47+
// time: 2020-05-04
48+
49+
package removeoutmostparentheses
50+
51+
// time complexity: O(n), where n is length of S.
52+
// space complexity: O(1)
53+
func removeOuterParentheses(S string) string {
54+
// S is valid parentheses string, so, s is empty "" or starts with "("
55+
if len(S) == 0 {
56+
return S
57+
}
58+
59+
var stackLength, left int
60+
61+
var ret string
62+
63+
for i := 0; i < len(S); i++ {
64+
if stackLength == 0 {
65+
left = i
66+
}
67+
if S[i] == '(' {
68+
stackLength++
69+
} else {
70+
stackLength--
71+
}
72+
if stackLength == 0 {
73+
ret += S[left+1 : i]
74+
}
75+
}
76+
return ret
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package removeoutmostparentheses
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestRemoveOuterParentheses(t *testing.T) {
8+
testCases := []map[string]string{
9+
{"case": "(()())(())", "expected": "()()()"},
10+
{"case": "(()())(())(()(()))", "expected": "()()()()(())"},
11+
{"case": "", "expected": ""},
12+
{"case": "()()", "expected": ""},
13+
}
14+
15+
for _, testCase := range testCases {
16+
if testCase["expected"] != removeOuterParentheses(testCase["case"]) {
17+
t.Errorf("hello")
18+
}
19+
}
20+
}

src/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,5 @@
114114
|0728|[Self Dividing Numbers](./0728_self_dividing_numbers/self_dividing_numbers.go)|Easy||
115115
|0735|[735. Asteroid Collision](0735_asteroid_collision/ac.go)|Medium|*`stack`*|
116116
|0747|[Largest Number At Least Twice of Others](./0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)|Easy||
117-
|0872|[872. Leaf-Similar Trees](0872_leaf_similar_trees/leaf_similar_trees.go)|Easy|*`binary tree`*|
117+
|0872|[872. Leaf-Similar Trees](0872_leaf_similar_trees/leaf_similar_trees.go)|Easy|*`binary tree`*|
118+
|1021|[1021. Remove Outermost Parentheses](1021_Remove_Outermost_Parentheses/remove_outmost_parentheses.go)|Easy|*`stack`*|

0 commit comments

Comments
 (0)