Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

1021. Remove Outermost Parentheses #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from 1021
May 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ continually updating 😃.
* [345. Reverse Vowels of a String](src/0345_reverse_vowels_of_a_string/reverse_vowels.go)   *`string;`*  *`double index`*
* [438. Find All Anagrams in a String](src/0438_all_anagrams_in_a_string/all_anagrams_in_a_string.go)   *`sliding window`*
* [557. Reverse Words in a String III](src/0557_reverse_words_in_a_string_3/reverse_words_in_a_string_3.go)
* [1021. Remove Outermost Parentheses](src/1021_Remove_Outermost_Parentheses/remove_outmost_parentheses.go)   *`stack`*

### Linked List
* [2. Add Two Numbers](src/0002_add_two_numbers/add_two_numbers.go)   *`recursion;`*  *`math`*
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
1021. Remove Outermost Parentheses

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.

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.

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.

Return S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.



Example 1:

Input: "(()())(())"
Output: "()()()"
Explanation:
The input string is "(()())(())", with primitive decomposition "(()())" + "(())".
After removing outer parentheses of each part, this is "()()" + "()" = "()()()".
Example 2:

Input: "(()())(())(()(()))"
Output: "()()()()(())"
Explanation:
The input string is "(()())(())(()(()))", with primitive decomposition "(()())" + "(())" + "(()(()))".
After removing outer parentheses of each part, this is "()()" + "()" + "()(())" = "()()()()(())".
Example 3:

Input: "()()"
Output: ""
Explanation:
The input string is "()()", with primitive decomposition "()" + "()".
After removing outer parentheses of each part, this is "" + "" = "".


Note:

S.length <= 10000
S[i] is "(" or ")"
S is a valid parentheses string



*/

// time: 2020年05月04日

package removeoutmostparentheses

// time complexity: O(n), where n is length of S.
// space complexity: O(1)
func removeOuterParentheses(S string) string {
// S is valid parentheses string, so, s is empty "" or starts with "("
if len(S) == 0 {
return S
}

var stackLength, left int

var ret string

for i := 0; i < len(S); i++ {
if stackLength == 0 {
left = i
}
if S[i] == '(' {
stackLength++
} else {
stackLength--
}
if stackLength == 0 {
ret += S[left+1 : i]
}
}
return ret
}
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package removeoutmostparentheses

import (
"testing"
)

func TestRemoveOuterParentheses(t *testing.T) {
testCases := []map[string]string{
{"case": "(()())(())", "expected": "()()()"},
{"case": "(()())(())(()(()))", "expected": "()()()()(())"},
{"case": "", "expected": ""},
{"case": "()()", "expected": ""},
}

for _, testCase := range testCases {
if testCase["expected"] != removeOuterParentheses(testCase["case"]) {
t.Errorf("hello")
}
}
}
3 changes: 2 additions & 1 deletion src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@
|0728|[Self Dividing Numbers](./0728_self_dividing_numbers/self_dividing_numbers.go)|Easy||
|0735|[735. Asteroid Collision](0735_asteroid_collision/ac.go)|Medium|*`stack`*|
|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||
|0872|[872. Leaf-Similar Trees](0872_leaf_similar_trees/leaf_similar_trees.go)|Easy|*`binary tree`*|
|0872|[872. Leaf-Similar Trees](0872_leaf_similar_trees/leaf_similar_trees.go)|Easy|*`binary tree`*|
|1021|[1021. Remove Outermost Parentheses](1021_Remove_Outermost_Parentheses/remove_outmost_parentheses.go)|Easy|*`stack`*|

AltStyle によって変換されたページ (->オリジナル) /