Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit dddf3b8

Browse files
aQuaaQua
aQua
authored and
aQua
committed
678 added
1 parent 87c95ff commit dddf3b8

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# [678. Valid Parenthesis String](https://leetcode.com/problems/valid-parenthesis-string/)
2+
3+
## 题目
4+
5+
Given a string containing only three types of characters: '(', ')' and '*', write a function to check whether this string is valid. We define the validity of a string by these rules:
6+
7+
1. Any left parenthesis '(' must have a corresponding right parenthesis ')'.
8+
1. Any right parenthesis ')' must have a corresponding left parenthesis '('.
9+
1. Left parenthesis '(' must go before the corresponding right parenthesis ')'.
10+
1. '*' could be treated as a single right parenthesis ')' or a single left parenthesis '(' or an empty string.
11+
1. An empty string is also valid.
12+
13+
Example 1:
14+
15+
```text
16+
Input: "()"
17+
Output: True
18+
```
19+
20+
Example 2:
21+
22+
```text
23+
Input: "(*)"
24+
Output: True
25+
```
26+
27+
Example 3:
28+
29+
```text
30+
Input: "(*))"
31+
Output: True
32+
```
33+
34+
Note:
35+
36+
1. The string size will be in the range [1, 100].
37+
38+
## 解题思路
39+
40+
见程序注释
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Problem0678
2+
3+
func checkValidString(s string) bool {
4+
res := true
5+
6+
return res
7+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package Problem0678
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
// tcs is testcase slice
11+
var tcs = []struct {
12+
s string
13+
ans bool
14+
}{
15+
16+
{
17+
"()",
18+
true,
19+
},
20+
21+
{
22+
"(*)",
23+
true,
24+
},
25+
26+
{
27+
"(*))",
28+
true,
29+
},
30+
31+
// 可以有多个 testcase
32+
}
33+
34+
func Test_fn(t *testing.T) {
35+
ast := assert.New(t)
36+
37+
for _, tc := range tcs {
38+
fmt.Printf("~~%v~~\n", tc)
39+
ast.Equal(tc.ans, checkValidString(tc.s), "输入:%v", tc)
40+
}
41+
}
42+
43+
func Benchmark_fn(b *testing.B) {
44+
for i := 0; i < b.N; i++ {
45+
for _, tc := range tcs {
46+
checkValidString(tc.s)
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)