Skip to content

Commit c6f5d34

Browse files
authored
Merge pull request #835 from 0xff-dev/1249
Add solution and test-cases for problem 1249
2 parents 6e2ccbc + de912f4 commit c6f5d34

File tree

3 files changed

+66
-22
lines changed

3 files changed

+66
-22
lines changed

leetcode/1201-1300/1249.Minimum-Remove-to-Make-Valid-Parentheses/README.md

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,39 @@
11
# [1249.Minimum Remove to Make Valid Parentheses][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
Given a string s of `'('` , `')'` and lowercase English characters.
5+
6+
Your task is to remove the minimum number of parentheses ( `'('` or `')'`, in any positions ) so that the resulting parentheses string is valid and return **ans** valid string.
7+
8+
Formally, a parentheses string is valid if and only if:
9+
10+
- It is the empty string, contains only lowercase characters, or
11+
- It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are valid strings, or
12+
- It can be written as `(A)`, where `A` is a valid string.
13+
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: s = "lee(t(c)o)de)"
19+
Output: "lee(t(c)o)de"
20+
Explanation: "lee(t(co)de)" , "lee(t(c)ode)" would also be accepted.
1321
```
1422

15-
## 题意
16-
> ...
23+
**Example 2:**
1724

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Minimum Remove to Make Valid Parentheses
23-
```go
2425
```
26+
Input: s = "a)b(c)d"
27+
Output: "ab(c)d"
28+
```
29+
30+
**Example 3:**
2531

32+
```
33+
Input: s = "))(("
34+
Output: ""
35+
Explanation: An empty string is also valid.
36+
```
2637

2738
## 结语
2839

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "strings"
4+
5+
func Solution(s string) string {
6+
left := -1
7+
stack := make([]int, len(s))
8+
removes := make([]int, 0)
9+
for i := range s {
10+
if s[i] == '(' {
11+
left++
12+
stack[left] = i
13+
continue
14+
}
15+
if s[i] == ')' {
16+
if left >= 0 {
17+
left--
18+
} else {
19+
removes = append(removes, i)
20+
}
21+
}
22+
}
23+
check := make(map[int]struct{})
24+
for i := 0; i <= left; i++ {
25+
check[stack[i]] = struct{}{}
26+
}
27+
for _, idx := range removes {
28+
check[idx] = struct{}{}
29+
}
30+
buf := strings.Builder{}
31+
for i := 0; i < len(s); i++ {
32+
if _, ok := check[i]; ok {
33+
continue
34+
}
35+
buf.WriteByte(s[i])
36+
}
37+
return buf.String()
538
}

leetcode/1201-1300/1249.Minimum-Remove-to-Make-Valid-Parentheses/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "lee(t(c)o)de)", "lee(t(c)o)de"},
17+
{"TestCase2", "a)b(c)d", "ab(c)d"},
18+
{"TestCase3", ")))(((", ""},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)