Skip to content

Commit 6e2ccbc

Browse files
authored
Merge pull request #836 from 0xff-dev/678
Add solution and test-cases for problem 678
2 parents f6c248b + 478e8fc commit 6e2ccbc

File tree

3 files changed

+74
-21
lines changed

3 files changed

+74
-21
lines changed

leetcode/601-700/0678.Valid-Parenthesis-String/README.md

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,35 @@
11
# [678.Valid Parenthesis String][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` containing only three types of characters: `'('`, `')'` and `'*'`, return `true` if `s` is **valid**.
5+
6+
The following rules define a **valid** string:
7+
8+
- Any left parenthesis `'('` must have a corresponding right parenthesis `')'`.
9+
- Any right parenthesis `')'` must have a corresponding left parenthesis `'('`.
10+
- Left parenthesis `'('` must go before the corresponding right parenthesis `')'`.
11+
- `'*'` could be treated as a single right parenthesis `')'` or a single left parenthesis `'('` or an empty string `""`.
712

813
**Example 1:**
914

1015
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
16+
Input: s = "()"
17+
Output: true
1318
```
1419

15-
## 题意
16-
> ...
17-
18-
## 题解
20+
**Example 2:**
1921

20-
### 思路1
21-
> ...
22-
Valid Parenthesis String
23-
```go
2422
```
23+
Input: s = "(*)"
24+
Output: true
25+
```
26+
27+
**Example 3:**
2528

29+
```
30+
Input: s = "(*))"
31+
Output: true
32+
```
2633

2734
## 结语
2835

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) bool {
4+
// 尝试将*替换成 “”,(, ),来做判断, 就是一个dfs+cache,但是问题是,*可能会非常多
5+
// 例如******* 一共100个,每个搜有三种选择,就离谱了啊, 先试试
6+
l := len(s)
7+
cache := make([][]int8, l+1)
8+
for i := 0; i <= l; i++ {
9+
cache[i] = make([]int8, l+1)
10+
for j := 0; j <= l; j++ {
11+
cache[i][j] = -1
12+
}
13+
}
14+
var dfs func(int, int) bool
15+
dfs = func(index, left int) bool {
16+
if index == l {
17+
return left == 0
18+
}
19+
20+
if cache[index][left] != -1 {
21+
return cache[index][left] == 1
22+
}
23+
24+
r := false
25+
if s[index] == '*' {
26+
// 如果 将* 设置为(
27+
r = r || dfs(index+1, left+1)
28+
// 注意只有说有(的时候,设置为)才是正确的
29+
if left > 0 {
30+
r = r || dfs(index+1, left-1)
31+
}
32+
r = r || dfs(index+1, left)
33+
} else {
34+
if s[index] == '(' {
35+
r = dfs(index+1, left+1)
36+
} else {
37+
if left > 0 {
38+
r = dfs(index+1, left-1)
39+
}
40+
}
41+
}
42+
if r {
43+
cache[index][left] = 1
44+
} else {
45+
cache[index][left] = 0
46+
}
47+
return r
48+
}
49+
return dfs(0, 0)
550
}

leetcode/601-700/0678.Valid-Parenthesis-String/Solution_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
13+
inputs string
1414
expect bool
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "()", true},
17+
{"TestCase2", "(*)", true},
18+
{"TestCase3", "(*))", true},
19+
{"TestCase4", "************************************************************", true},
1920
}
2021

2122
// 开始测试
@@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
3031
}
3132
}
3233

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

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

0 commit comments

Comments
 (0)