Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package remove_outermost_parentheses

func removeOuterParentheses(S string) string {
ans, n := make([]rune, 0, len(S)), 0
for _, c := range S {
if c == '(' {
n++
} else {
n--
}
if (c == '(' && n != 1) || (c == ')' && n != 0) {
ans = append(ans, c)
}
}
return string(ans)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package remove_outermost_parentheses

import "testing"

type caseType struct {
input string
expected string
}

func TestRemoveOuterParentheses(t *testing.T) {
tests := [...]caseType{
{
input: "(()())(())",
expected: "()()()",
},
{
input: "(()())(())(()(()))",
expected: "()()()()(())",
},
{
input: "()()",
expected: "",
},
}
for _, tc := range tests {
output := removeOuterParentheses(tc.input)
if output != tc.expected {
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
}
}
}