-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3703-RemoveKBalancedSubstrings.go
More file actions
181 lines (165 loc) · 5.33 KB
/
3703-RemoveKBalancedSubstrings.go
File metadata and controls
181 lines (165 loc) · 5.33 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
// 3703. Remove K-Balanced Substrings
// You are given a string s consisting of '(' and ')', and an integer k.
// A string is k-balanced if it is exactly k consecutive '(' followed by k consecutive ')', i.e., '(' * k + ')' * k.
// For example, if k = 3, k-balanced is "((()))".
// You must repeatedly remove all non-overlapping k-balanced substrings from s, and then join the remaining parts.
// Continue this process until no k-balanced substring exists.
// Return the final string after all possible removals.
// A substring is a contiguous non-empty sequence of characters within a string.
// Example 1:
// Input: s = "(())", k = 1
// Output: ""
// Explanation:
// k-balanced substring is "()"
// Step Current s k-balanced Result s
// 1 (()) (()) ()
// 2 () () Empty
// Thus, the final string is "".
// Example 2:
// Input: s = "(()(", k = 1
// Output: "(("
// Explanation:
// k-balanced substring is "()"
// Step Current s k-balanced Result s
// 1 (()( (()( ((
// 2 (( - ((
// Thus, the final string is "((".
// Example 3:
// Input: s = "((()))()()()", k = 3
// Output: "()()()"
// Explanation:
// k-balanced substring is "((()))"
// Step Current s k-balanced Result s
// 1 ((()))()()() ((()))()()() ()()()
// 2 ()()() - ()()()
// Thus, the final string is "()()()".
// Constraints:
// 2 <= s.length <= 10^5
// s consists only of '(' and ')'.
// 1 <= k <= s.length / 2
import "fmt"
// time: O(n), space: O(n)
func removeSubstring(s string, k int) string {
res, n := []byte{}, len(s)
check := func(i int) byte {
if 0 <= i && i < len(res) {
return res[i]
}
return 0
}
left, right := 0, 0
for si, ri := 0, 0; si <= n; si, ri = si + 1, ri + 1 {
if si < n {
res = append(res, s[si])
}
if check(ri) == ')' {
right++
}
if _c := check(ri-k); _c == ')' {
right--
} else if _c == '(' {
left++
}
if check(ri-k*2) == '(' {
left--
}
if left == k && right == k {
l := len(res) - k * 2
res = res[:l]
ri = l - 1
left, right = 0, 0
for j, e := ri, max(0, l - k); j >= e; j-- {
if check(j) == ')' {
right++
}
if check(j - k) == '(' {
left++
}
}
}
}
return string(res)
}
func removeSubstring1(s string, k int) string {
stack, cl , cr := []byte{}, 0, 0
for i := range s {
x := s[i]
if x == '('{
if len(stack) > 0 && stack[len(stack) - 1] == '('{
cl++
} else {
cl = 1
}
stack = append(stack,x)
} else {
if len(stack)>0 && stack[len(stack) - 1] == ')' {
cr++
} else {
cr = 1
}
stack = append(stack,x)
if cr >= k && cl >= k {
stack = stack[:len(stack) - 2 * k]
cl -= k
cr -= k
if cl == 0 {
for j := len(stack) - 1; j >= 0; j-- {
y := stack[j]
if y == ')' && cl == 0 {
cr++
}
if y == ')' && cl > 0 {
break
}
if y == '(' {
cl++
}
}
}
}
}
}
return string(stack)
}
func main() {
// Example 1:
// Input: s = "(())", k = 1
// Output: ""
// Explanation:
// k-balanced substring is "()"
// Step Current s k-balanced Result s
// 1 (()) (()) ()
// 2 () () Empty
// Thus, the final string is "".
fmt.Println(removeSubstring("(())", 1)) // ""
// Example 2:
// Input: s = "(()(", k = 1
// Output: "(("
// Explanation:
// k-balanced substring is "()"
// Step Current s k-balanced Result s
// 1 (()( (()( ((
// 2 (( - ((
// Thus, the final string is "((".
fmt.Println(removeSubstring("(()(", 1)) // "(("
// Example 3:
// Input: s = "((()))()()()", k = 3
// Output: "()()()"
// Explanation:
// k-balanced substring is "((()))"
// Step Current s k-balanced Result s
// 1 ((()))()()() ((()))()()() ()()()
// 2 ()()() - ()()()
// Thus, the final string is "()()()".
fmt.Println(removeSubstring("((()))()()()", 3)) // "()()()"
fmt.Println(removeSubstring("((()))((()))", 3)) // ""
fmt.Println(removeSubstring("()()()()()()", 3)) // "()()()()()()"
fmt.Println(removeSubstring("((()))((()))((()))", 3)) // ""
fmt.Println(removeSubstring1("(())", 1)) // ""
fmt.Println(removeSubstring1("(()(", 1)) // "(("
fmt.Println(removeSubstring1("((()))()()()", 3)) // "()()()"
fmt.Println(removeSubstring1("((()))((()))", 3)) // ""
fmt.Println(removeSubstring1("()()()()()()", 3)) // "()()()()()()"
fmt.Println(removeSubstring1("((()))((()))((()))", 3)) // ""
}