HTMLify
LeetCode - Remove Outermost Parentheses - Go
Views: 317 | Author: abh
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 | func valid(stack []rune) bool { var _s []rune for _, p := range stack { if p == '(' { _s = append(_s, p) } else { if _s[len(_s)-1] == '(' { _s = _s[:len(_s)-1] } } } return len(_s) == 0 } func removeOuterParentheses(s string) string { var stack []rune var ans string for _, p := range s { stack = append(stack, p) if valid(stack) { for _, _p := range stack[1:len(stack)-1] { ans += string(_p) } stack = nil } } return ans } |