diff --git a/leetcode/601-700/0678.Valid-Parenthesis-String/README.md b/leetcode/601-700/0678.Valid-Parenthesis-String/README.md index 8f58da954..6571f5376 100644 --- a/leetcode/601-700/0678.Valid-Parenthesis-String/README.md +++ b/leetcode/601-700/0678.Valid-Parenthesis-String/README.md @@ -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 +``` ## 结语 diff --git a/leetcode/601-700/0678.Valid-Parenthesis-String/Solution.go b/leetcode/601-700/0678.Valid-Parenthesis-String/Solution.go index d115ccf5e..ec407b3cd 100644 --- a/leetcode/601-700/0678.Valid-Parenthesis-String/Solution.go +++ b/leetcode/601-700/0678.Valid-Parenthesis-String/Solution.go @@ -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) } diff --git a/leetcode/601-700/0678.Valid-Parenthesis-String/Solution_test.go b/leetcode/601-700/0678.Valid-Parenthesis-String/Solution_test.go index 14ff50eb4..86cb400d7 100644 --- a/leetcode/601-700/0678.Valid-Parenthesis-String/Solution_test.go +++ b/leetcode/601-700/0678.Valid-Parenthesis-String/Solution_test.go @@ -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}, } // 开始测试 @@ -30,10 +31,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }