-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1021.go
35 lines (32 loc) · 803 Bytes
/
1021.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main
import (
"fmt"
)
func removeOuterParentheses(S string) string {
var top string
stack := make([]byte, 0)
index, sIndex := 0, 0
for i := 0; i < len(S); i++ {
if S[i] == '(' { // handle '('
stack = append(stack, S[i])
} else { // handle ')'
//pop left parenthese, no stack range check
stack = stack[:len(stack)-1]
if len(stack) == index {
//drop outer parenthese
top = S[sIndex+1 : i]
stack = append(stack, top...)
index += len(top)
sIndex = i + 1
}
}
}
return string(stack)
}
func main() {
fmt.Println(removeOuterParentheses("(()())(())(()(()))"))
fmt.Println(removeOuterParentheses("(()(()))"))
fmt.Println(removeOuterParentheses("(()())(())"))
fmt.Println(removeOuterParentheses("()()"))
fmt.Println(removeOuterParentheses("(())"))
}