-
Notifications
You must be signed in to change notification settings - Fork 46
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
src/1021_Remove_Outermost_Parentheses/remove_outmost_parentheses.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
20 changes: 20 additions & 0 deletions
src/1021_Remove_Outermost_Parentheses/remove_outmost_parentheses_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.