diff --git a/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/1.png b/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/1.png new file mode 100644 index 000000000..11fc2d2a0 Binary files /dev/null and b/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/1.png differ diff --git a/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/README.md b/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/README.md index 9448f27aa..4b3a8a33e 100755 --- a/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/README.md +++ b/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/README.md @@ -1,28 +1,46 @@ # [2116.Check if a Parentheses String Can Be Valid][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 +A parentheses string is a **non-empty** string consisting only of `'('` and `')'`. It is valid if **any** of the following conditions is **true**: + +- It is `()`. +- It can be written as `AB` (`A` concatenated with `B`), where `A` and `B` are valid parentheses strings. +- It can be written as `(A)`, where `A` is a valid parentheses string. + +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`, + +- If `locked[i]` is `'1'`, you **cannot** change `s[i]`. +- But if `locked[i]` is `'0'`, you **can** change `s[i]` to either `'('` or `')'`. + +Return `true` if you can make `s` a valid parentheses string. Otherwise, return `false`. + +**Example 1:** -**Example 1:** +![1](./1.png) ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "))()))", locked = "010100" +Output: true +Explanation: locked[1] == '1' and locked[3] == '1', so we cannot change s[1] or s[3]. +We change s[0] and s[4] to '(' while leaving s[2] and s[5] unchanged to make s valid. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Check if a Parentheses String Can Be Valid -```go ``` +Input: s = "()()", locked = "0000" +Output: true +Explanation: We do not need to make any changes because s is already valid. +``` + +**Example 3:** +``` +Input: s = ")", locked = "0" +Output: false +Explanation: locked permits us to change s[0]. +Changing s[0] to either '(' or ')' will not make s valid. +``` ## 结语 diff --git a/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/Solution.go b/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/Solution.go index d115ccf5e..350e0b612 100644 --- a/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/Solution.go +++ b/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/Solution.go @@ -1,5 +1,38 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string, locked string) bool { + l := len(s) + if l&1 == 1 { + return false + } + stack1, stack2 := make([]int, l), make([]int, l) + i1, i2 := -1, -1 + for i, b := range s { + if locked[i] == '0' { + i1++ + stack1[i1] = i + continue + } + if b == '(' { + i2++ + stack2[i2] = i + continue + } + if i2 != -1 { + i2-- + continue + } + if i1 != -1 { + i1-- + continue + } + return false + } + for i2 >= 0 && i1 >= 0 { + if stack2[i2] > stack1[i1] { + break + } + i2, i1 = i2-1, i1-1 + } + return i2 == -1 } diff --git a/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/Solution_test.go b/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/Solution_test.go index 14ff50eb4..d13eec50a 100644 --- a/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/Solution_test.go +++ b/leetcode/2101-2200/2116.Check-if-a-Parentheses-String-Can-Be-Valid/Solution_test.go @@ -10,30 +10,31 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool + inputs string + locked string expect bool }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "))()))", "010100", true}, + {"TestCase2", "()()", "0000", true}, + {"TestCase3", ")", "0", false}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.inputs, c.locked) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.inputs, c.locked) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }