-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1249-MinimumRemoveToMakeValidParentheses.go
More file actions
107 lines (97 loc) · 3.31 KB
/
1249-MinimumRemoveToMakeValidParentheses.go
File metadata and controls
107 lines (97 loc) · 3.31 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package main
// 1249. Minimum Remove to Make Valid Parentheses
// Given a string s of '(' , ')' and lowercase English characters.
// Your task is to remove the minimum number of parentheses ( '(' or ')', in any positions ) so that the resulting parentheses string is valid and return any valid string.
// Formally, a parentheses string is valid if and only if:
// It is the empty string, contains only lowercase characters, or
// It can be written as AB (A concatenated with B), where A and B are valid strings, or
// It can be written as (A), where A is a valid string.
// Example 1:
// Input: s = "lee(t(c)o)de)"
// Output: "lee(t(c)o)de"
// Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
// Example 2:
// Input: s = "a)b(c)d"
// Output: "ab(c)d"
// Example 3:
// Input: s = "))(("
// Output: ""
// Explanation: An empty string is also valid.
// Constraints:
// 1 <= s.length <= 10^5
// s[i] is either'(' , ')', or lowercase English letter.
import "fmt"
import "strings"
// stack
func minRemoveToMakeValid(s string) string {
stack, todel := make([]int,0), make([]int,0)
for i, c := range s {
if c == '('{
// 入栈 push
stack = append(stack, i)
continue
}
if c == ')'{
// 如果 没有 ( 就出现了 ) 加入到清除列表
if len(stack) <= 0 {
todel = append(todel, i)
} else {
// 出栈 pop
stack = stack[:len(stack)-1]
}
}
}
todel = append(todel, stack...) // 只有 ( 没有 )的都加入到待删除列表中
//fmt.Println(todel)
res := []byte{}
for i, j := 0, 0; j < len(s); j++{
if i < len(todel) && todel[i] == j {
i++
continue
}
res = append(res, s[j])
}
return string(res)
}
func minRemoveToMakeValid1(s string) string {
stack1, stack2, res := []int{}, []int{}, []byte(s)
for i := 0; i < len(s); i++ {
if s[i] == '(' { // 遇到 ( 入栈
stack1 = append(stack1, i)
} else if s[i] == ')' {
if len(stack1) > 0 {
stack1 = stack1[:len(stack1)-1] // 匹配到 ) 出栈
} else {
stack2 = append(stack2, i) // 没有配对的 ) 入栈到 stack2
}
}
}
// 将需要替换的 '(',')' 替换成 #
for _, i := range stack1 {
res[i] = '#'
}
for _, i := range stack2 {
res[i] = '#'
}
return strings.ReplaceAll(string(res), "#", "")
}
func main() {
// Example 1:
// Input: s = "lee(t(c)o)de)"
// Output: "lee(t(c)o)de"
// Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
fmt.Println(minRemoveToMakeValid("lee(t(co)de)")) // lee(t(co)de)
// Example 2:
// Input: s = "a)b(c)d"
// Output: "ab(c)d"
fmt.Println(minRemoveToMakeValid("a)b(c)d")) // ab(c)d
// Example 3:
// Input: s = "))(("
// Output: ""
// Explanation: An empty string is also valid.
// Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
fmt.Println(minRemoveToMakeValid("))((")) // ""
fmt.Println(minRemoveToMakeValid1("lee(t(co)de)")) // lee(t(co)de)
fmt.Println(minRemoveToMakeValid1("a)b(c)d")) // ab(c)d
fmt.Println(minRemoveToMakeValid1("))((")) // ""
}