-
Notifications
You must be signed in to change notification settings - Fork 0
/
301.go
65 lines (57 loc) · 1.25 KB
/
301.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package p301
/**
Remove the minimum number of invalid parentheses in order to make the input string valid.
Return all possible results.
Note: The input string may contain letters other than the parentheses ( and ).
Examples:
"()())()" -> ["()()()", "(())()"]
"(a)())()" -> ["(a)()()", "(a())()"]
")(" -> [""]
*/
const pars = '(' + ')'
type List struct {
slice []string
}
func reverse(s []byte) []byte {
res := make([]byte, len(s))
i, j := 0, len(s)-1
for i <= j {
res[i] = s[j]
res[j] = s[i]
i++
j--
}
return res
}
func removeInvalidParentheses(s string) []string {
list := List{slice: make([]string, 0)}
remove([]byte(s), &list, 0, 0, '(')
return list.slice
}
func remove(ss []byte, l *List, last_i, last_j int, par byte) {
for stack, i := 0, last_i; i < len(ss); i++ {
if ss[i] == par {
stack++
} else if ss[i] == pars-par {
stack--
}
if stack >= 0 {
continue
}
for j := last_j; j <= i; j++ {
if ss[j] == pars-par && (j == last_j || ss[j-1] != pars-par) {
nss := make([]byte, len(ss)-1)
copy(nss, ss[:j])
copy(nss[j:], ss[j+1:])
remove(nss, l, i, j, par)
}
}
return
}
rss := reverse(ss)
if par == '(' && len(rss) > 0 {
remove(rss, l, 0, 0, ')')
} else {
l.slice = append(l.slice, string(rss))
}
}