Skip to content

Add solution and test-cases for problem 678 #836

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions leetcode/601-700/0678.Valid-Parenthesis-String/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
# [678.Valid Parenthesis String][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
Given a string `s` containing only three types of characters: `'('`, `')'` and `'*'`, return `true` if `s` is **valid**.

The following rules define a **valid** string:

- Any left parenthesis `'('` must have a corresponding right parenthesis `')'`.
- Any right parenthesis `')'` must have a corresponding left parenthesis `'('`.
- Left parenthesis `'('` must go before the corresponding right parenthesis `')'`.
- `'*'` could be treated as a single right parenthesis `')'` or a single left parenthesis `'('` or an empty string `""`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "()"
Output: true
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Valid Parenthesis String
```go
```
Input: s = "(*)"
Output: true
```

**Example 3:**

```
Input: s = "(*))"
Output: true
```

## 结语

Expand Down
49 changes: 47 additions & 2 deletions leetcode/601-700/0678.Valid-Parenthesis-String/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(s string) bool {
// 尝试将*替换成 “”,(, ),来做判断, 就是一个dfs+cache,但是问题是,*可能会非常多
// 例如******* 一共100个,每个搜有三种选择,就离谱了啊, 先试试
l := len(s)
cache := make([][]int8, l+1)
for i := 0; i <= l; i++ {
cache[i] = make([]int8, l+1)
for j := 0; j <= l; j++ {
cache[i][j] = -1
}
}
var dfs func(int, int) bool
dfs = func(index, left int) bool {
if index == l {
return left == 0
}

if cache[index][left] != -1 {
return cache[index][left] == 1
}

r := false
if s[index] == '*' {
// 如果 将* 设置为(
r = r || dfs(index+1, left+1)
// 注意只有说有(的时候,设置为)才是正确的
if left > 0 {
r = r || dfs(index+1, left-1)
}
r = r || dfs(index+1, left)
} else {
if s[index] == '(' {
r = dfs(index+1, left+1)
} else {
if left > 0 {
r = dfs(index+1, left-1)
}
}
}
if r {
cache[index][left] = 1
} else {
cache[index][left] = 0
}
return r
}
return dfs(0, 0)
}
13 changes: 7 additions & 6 deletions leetcode/601-700/0678.Valid-Parenthesis-String/Solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
inputs string
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "()", true},
{"TestCase2", "(*)", true},
{"TestCase3", "(*))", true},
{"TestCase4", "************************************************************", true},
}

// 开始测试
Expand All @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}