Skip to content

Commit c8fd20a

Browse files
authored
Merge pull request #1092 from 0xff-dev/2116
Add solution and test-cases for problem 2116
2 parents 544046f + eee9b16 commit c8fd20a

File tree

4 files changed

+77
-25
lines changed

4 files changed

+77
-25
lines changed
Loading

leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/README.md

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
# [2116.Check if a Parentheses String Can Be Valid][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+
A parentheses string is a **non-empty** string consisting only of `'('` and `')'`. It is valid if **any** of the following conditions is **true**:
5+
6+
- It is `()`.
7+
- It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are valid parentheses strings.
8+
- It can be written as `(A)`, where `A` is a valid parentheses string.
9+
10+
You are given a parentheses string `s` and a string `locked`, both of length `n`. `locked` is a binary string consisting only of `'0'`s and `'1'`s. For **each** index `i` of `locked`,
11+
12+
- If `locked[i]` is `'1'`, you **cannot** change `s[i]`.
13+
- But if `locked[i]` is `'0'`, you **can** change `s[i]` to either `'('` or `')'`.
14+
15+
Return `true` if you can make `s` a valid parentheses string. Otherwise, return `false`.
16+
17+
**Example 1:**
718

8-
**Example 1:**
19+
![1](./1.png)
920

1021
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
22+
Input: s = "))()))", locked = "010100"
23+
Output: true
24+
Explanation: locked[1] == '1' and locked[3] == '1', so we cannot change s[1] or s[3].
25+
We change s[0] and s[4] to '(' while leaving s[2] and s[5] unchanged to make s valid.
1326
```
1427

15-
## 题意
16-
> ...
28+
**Example 2:**
1729

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Check if a Parentheses String Can Be Valid
23-
```go
2430
```
31+
Input: s = "()()", locked = "0000"
32+
Output: true
33+
Explanation: We do not need to make any changes because s is already valid.
34+
```
35+
36+
**Example 3:**
2537

38+
```
39+
Input: s = ")", locked = "0"
40+
Output: false
41+
Explanation: locked permits us to change s[0].
42+
Changing s[0] to either '(' or ')' will not make s valid.
43+
```
2644

2745
## 结语
2846

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+
func Solution(s string, locked string) bool {
4+
l := len(s)
5+
if l&1 == 1 {
6+
return false
7+
}
8+
stack1, stack2 := make([]int, l), make([]int, l)
9+
i1, i2 := -1, -1
10+
for i, b := range s {
11+
if locked[i] == '0' {
12+
i1++
13+
stack1[i1] = i
14+
continue
15+
}
16+
if b == '(' {
17+
i2++
18+
stack2[i2] = i
19+
continue
20+
}
21+
if i2 != -1 {
22+
i2--
23+
continue
24+
}
25+
if i1 != -1 {
26+
i1--
27+
continue
28+
}
29+
return false
30+
}
31+
for i2 >= 0 && i1 >= 0 {
32+
if stack2[i2] > stack1[i1] {
33+
break
34+
}
35+
i2, i1 = i2-1, i1-1
36+
}
37+
return i2 == -1
538
}

leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/Solution_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,31 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs string
14+
locked string
1415
expect bool
1516
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
17+
{"TestCase1", "))()))", "010100", true},
18+
{"TestCase2", "()()", "0000", true},
19+
{"TestCase3", ")", "0", false},
1920
}
2021

2122
// 开始测试
2223
for i, c := range cases {
2324
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
25+
got := Solution(c.inputs, c.locked)
2526
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
28+
c.expect, got, c.inputs, c.locked)
2829
}
2930
})
3031
}
3132
}
3233

33-
// 压力测试
34+
// 压力测试
3435
func BenchmarkSolution(b *testing.B) {
3536
}
3637

37-
// 使用案列
38+
// 使用案列
3839
func ExampleSolution() {
3940
}

0 commit comments

Comments
 (0)