Skip to content

Commit 491d1a4

Browse files
committed
feat: add solutions to lc problem: No.1249
No.1249.Minimum Remove to Make Valid Parentheses
1 parent e5bf058 commit 491d1a4

File tree

6 files changed

+373
-21
lines changed

6 files changed

+373
-21
lines changed

solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/README.md

Lines changed: 134 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,16 @@
5858

5959
<!-- 这里可写通用的实现逻辑 -->
6060

61-
主体分为两步:
62-
63-
1. 遍历字符串 `s`,统计其中**成对**的括号数量。流程:
64-
1. 从前往后遍历 `s`,统计 `(` 的数量。
65-
2. 当遇到 `)` 时,对比当前 `(` 记录的数量,在 `)` 统计数量不超过 `(` 时才可算有效的数据。
66-
> `)` 有效的前提:在其前方存在对应的 `(`
67-
2. 再次遍历,生成返回值。流程:
68-
1. 准备一变量,记录加入返回值当中 `(` 的数量。
69-
2. 遍历字符串 `s`,流程:
70-
- 遇到 `(`
71-
- 确定当前还缺少成对括号时(查看成对统计数据),才让其加入,并使 `(` 统计数量 + 1。
72-
- 遇到 `)`
73-
- 确定前方存在 `(`(查看 `(` 统计数量)时,才让其加入,并删减 `(` 统计数据与成对数量(-1 操作)。
74-
- 其他字符串正常加入。
75-
76-
疑问:为什么要同时调整 `(` 统计数据与成对数量?
77-
78-
- 需要确保前方存在可用的 `(`,才能安心放入 `)`
79-
- 而删减成对数量是防止放入过量的 `(`
61+
**方法一:两遍扫描**
62+
63+
先从左向右扫描,将多余的右括号删除,再从右向左扫描,将多余的左括号删除。
64+
65+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。
66+
67+
相似题目:
68+
69+
- [678. 有效的括号字符串](/solution/0600-0699/0678.Valid%20Parenthesis%20String/README.md)
70+
- [2116. 判断一个括号字符串是否有效](/solution/2100-2199/2116.Check%20if%20a%20Parentheses%20String%20Can%20Be%20Valid/README.md)
8071

8172
<!-- tabs:start -->
8273

@@ -85,15 +76,138 @@
8576
<!-- 这里可写当前语言的特殊实现逻辑 -->
8677

8778
```python
88-
79+
class Solution:
80+
def minRemoveToMakeValid(self, s: str) -> str:
81+
stk = []
82+
x = 0
83+
for c in s:
84+
if c == ')' and x == 0:
85+
continue
86+
if c == '(':
87+
x += 1
88+
elif c == ')':
89+
x -= 1
90+
stk.append(c)
91+
x = 0
92+
ans = []
93+
for c in stk[::-1]:
94+
if c == '(' and x == 0:
95+
continue
96+
if c == ')':
97+
x += 1
98+
elif c == '(':
99+
x -= 1
100+
ans.append(c)
101+
return ''.join(ans[::-1])
89102
```
90103

91104
### **Java**
92105

93106
<!-- 这里可写当前语言的特殊实现逻辑 -->
94107

95108
```java
109+
class Solution {
110+
public String minRemoveToMakeValid(String s) {
111+
Deque<Character> stk = new ArrayDeque<>();
112+
int x = 0;
113+
for (int i = 0; i < s.length(); ++i) {
114+
char c = s.charAt(i);
115+
if (c == ')' && x == 0) {
116+
continue;
117+
}
118+
if (c == '(') {
119+
++x;
120+
} else if (c == ')') {
121+
--x;
122+
}
123+
stk.push(c);
124+
}
125+
StringBuilder ans = new StringBuilder();
126+
x = 0;
127+
while (!stk.isEmpty()) {
128+
char c = stk.pop();
129+
if (c == '(' && x == 0) {
130+
continue;
131+
}
132+
if (c == ')') {
133+
++x;
134+
} else if (c == '(') {
135+
--x;
136+
}
137+
ans.append(c);
138+
}
139+
return ans.reverse().toString();
140+
}
141+
}
142+
```
143+
144+
### **C++**
145+
146+
```cpp
147+
class Solution {
148+
public:
149+
string minRemoveToMakeValid(string s) {
150+
string stk;
151+
int x = 0;
152+
for (char& c : s) {
153+
if (c == ')' && x == 0) continue;
154+
if (c == '(') ++x;
155+
else if (c == ')') --x;
156+
stk.push_back(c);
157+
}
158+
string ans;
159+
x = 0;
160+
while (stk.size()) {
161+
char c = stk.back();
162+
stk.pop_back();
163+
if (c == '(' && x == 0) continue;
164+
if (c == ')') ++x;
165+
else if (c == '(') --x;
166+
ans.push_back(c);
167+
}
168+
reverse(ans.begin(), ans.end());
169+
return ans;
170+
}
171+
};
172+
```
96173
174+
### **Go**
175+
176+
```go
177+
func minRemoveToMakeValid(s string) string {
178+
stk := []byte{}
179+
x := 0
180+
for i := range s {
181+
c := s[i]
182+
if c == ')' && x == 0 {
183+
continue
184+
}
185+
if c == '(' {
186+
x++
187+
} else if c == ')' {
188+
x--
189+
}
190+
stk = append(stk, c)
191+
}
192+
ans := []byte{}
193+
x = 0
194+
for i := len(stk) - 1; i >= 0; i-- {
195+
c := stk[i]
196+
if c == '(' && x == 0 {
197+
continue
198+
}
199+
if c == ')' {
200+
x++
201+
} else if c == '(' {
202+
x--
203+
}
204+
ans = append(ans, c)
205+
}
206+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
207+
ans[i], ans[j] = ans[j], ans[i]
208+
}
209+
return string(ans)
210+
}
97211
```
98212

99213
### **TypeScript**

solution/1200-1299/1249.Minimum Remove to Make Valid Parentheses/README_EN.md

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,136 @@
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def minRemoveToMakeValid(self, s: str) -> str:
60+
stk = []
61+
x = 0
62+
for c in s:
63+
if c == ')' and x == 0:
64+
continue
65+
if c == '(':
66+
x += 1
67+
elif c == ')':
68+
x -= 1
69+
stk.append(c)
70+
x = 0
71+
ans = []
72+
for c in stk[::-1]:
73+
if c == '(' and x == 0:
74+
continue
75+
if c == ')':
76+
x += 1
77+
elif c == '(':
78+
x -= 1
79+
ans.append(c)
80+
return ''.join(ans[::-1])
5981
```
6082

6183
### **Java**
6284

6385
```java
86+
class Solution {
87+
public String minRemoveToMakeValid(String s) {
88+
Deque<Character> stk = new ArrayDeque<>();
89+
int x = 0;
90+
for (int i = 0; i < s.length(); ++i) {
91+
char c = s.charAt(i);
92+
if (c == ')' && x == 0) {
93+
continue;
94+
}
95+
if (c == '(') {
96+
++x;
97+
} else if (c == ')') {
98+
--x;
99+
}
100+
stk.push(c);
101+
}
102+
StringBuilder ans = new StringBuilder();
103+
x = 0;
104+
while (!stk.isEmpty()) {
105+
char c = stk.pop();
106+
if (c == '(' && x == 0) {
107+
continue;
108+
}
109+
if (c == ')') {
110+
++x;
111+
} else if (c == '(') {
112+
--x;
113+
}
114+
ans.append(c);
115+
}
116+
return ans.reverse().toString();
117+
}
118+
}
119+
```
120+
121+
### **C++**
122+
123+
```cpp
124+
class Solution {
125+
public:
126+
string minRemoveToMakeValid(string s) {
127+
string stk;
128+
int x = 0;
129+
for (char& c : s) {
130+
if (c == ')' && x == 0) continue;
131+
if (c == '(') ++x;
132+
else if (c == ')') --x;
133+
stk.push_back(c);
134+
}
135+
string ans;
136+
x = 0;
137+
while (stk.size()) {
138+
char c = stk.back();
139+
stk.pop_back();
140+
if (c == '(' && x == 0) continue;
141+
if (c == ')') ++x;
142+
else if (c == '(') --x;
143+
ans.push_back(c);
144+
}
145+
reverse(ans.begin(), ans.end());
146+
return ans;
147+
}
148+
};
149+
```
64150
151+
### **Go**
152+
153+
```go
154+
func minRemoveToMakeValid(s string) string {
155+
stk := []byte{}
156+
x := 0
157+
for i := range s {
158+
c := s[i]
159+
if c == ')' && x == 0 {
160+
continue
161+
}
162+
if c == '(' {
163+
x++
164+
} else if c == ')' {
165+
x--
166+
}
167+
stk = append(stk, c)
168+
}
169+
ans := []byte{}
170+
x = 0
171+
for i := len(stk) - 1; i >= 0; i-- {
172+
c := stk[i]
173+
if c == '(' && x == 0 {
174+
continue
175+
}
176+
if c == ')' {
177+
x++
178+
} else if c == '(' {
179+
x--
180+
}
181+
ans = append(ans, c)
182+
}
183+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
184+
ans[i], ans[j] = ans[j], ans[i]
185+
}
186+
return string(ans)
187+
}
65188
```
66189

67190
### **TypeScript**
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public:
3+
string minRemoveToMakeValid(string s) {
4+
string stk;
5+
int x = 0;
6+
for (char& c : s) {
7+
if (c == ')' && x == 0) continue;
8+
if (c == '(') ++x;
9+
else if (c == ')') --x;
10+
stk.push_back(c);
11+
}
12+
string ans;
13+
x = 0;
14+
while (stk.size()) {
15+
char c = stk.back();
16+
stk.pop_back();
17+
if (c == '(' && x == 0) continue;
18+
if (c == ')') ++x;
19+
else if (c == '(') --x;
20+
ans.push_back(c);
21+
}
22+
reverse(ans.begin(), ans.end());
23+
return ans;
24+
}
25+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
func minRemoveToMakeValid(s string) string {
2+
stk := []byte{}
3+
x := 0
4+
for i := range s {
5+
c := s[i]
6+
if c == ')' && x == 0 {
7+
continue
8+
}
9+
if c == '(' {
10+
x++
11+
} else if c == ')' {
12+
x--
13+
}
14+
stk = append(stk, c)
15+
}
16+
ans := []byte{}
17+
x = 0
18+
for i := len(stk) - 1; i >= 0; i-- {
19+
c := stk[i]
20+
if c == '(' && x == 0 {
21+
continue
22+
}
23+
if c == ')' {
24+
x++
25+
} else if c == '(' {
26+
x--
27+
}
28+
ans = append(ans, c)
29+
}
30+
for i, j := 0, len(ans)-1; i < j; i, j = i+1, j-1 {
31+
ans[i], ans[j] = ans[j], ans[i]
32+
}
33+
return string(ans)
34+
}

0 commit comments

Comments
 (0)