diff --git a/leetcode/1601-1700/1653.Minimum-Deletions-to-Make-String-Balanced/README.md b/leetcode/1601-1700/1653.Minimum-Deletions-to-Make-String-Balanced/README.md index eeafab258..528e731a4 100755 --- a/leetcode/1601-1700/1653.Minimum-Deletions-to-Make-String-Balanced/README.md +++ b/leetcode/1601-1700/1653.Minimum-Deletions-to-Make-String-Balanced/README.md @@ -1,28 +1,29 @@ # [1653.Minimum Deletions to Make String Balanced][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 +You are given a string `s` consisting only of characters `'a'` and `'b'`. + +You can delete any number of characters in `s` to make `s` **balanced**. `s` is **balanced** if there is no pair of indices `(i,j)` such that `i < j` and `s[i] = 'b'` and `s[j]= 'a'`. + +Return the **minimum** number of deletions needed to make `s` **balanced**. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "aababbab" +Output: 2 +Explanation: You can either: +Delete the characters at 0-indexed positions 2 and 6 ("aababbab" -> "aaabbb"), or +Delete the characters at 0-indexed positions 3 and 6 ("aababbab" -> "aabbbb"). ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Minimum Deletions to Make String Balanced -```go ``` - +Input: s = "bbaaaaabb" +Output: 2 +Explanation: The only solution is to delete the first two characters. +``` ## 结语 diff --git a/leetcode/1601-1700/1653.Minimum-Deletions-to-Make-String-Balanced/Solution.go b/leetcode/1601-1700/1653.Minimum-Deletions-to-Make-String-Balanced/Solution.go index d115ccf5e..42980b230 100644 --- a/leetcode/1601-1700/1653.Minimum-Deletions-to-Make-String-Balanced/Solution.go +++ b/leetcode/1601-1700/1653.Minimum-Deletions-to-Make-String-Balanced/Solution.go @@ -1,5 +1,27 @@ package Solution -func Solution(x bool) bool { - return x +import "math" + +func Solution(s string) int { + l := len(s) + ac, bc := make([]int, l), make([]int, l) + b := 0 + for i := 0; i < l; i++ { + if s[i] == 'b' { + b++ + } + bc[i] = b + } + a := 0 + for i := l - 1; i >= 0; i-- { + if s[i] == 'a' { + a++ + } + ac[i] = a + } + ans := math.MaxInt + for i := 0; i < l; i++ { + ans = min(ans, ac[i]+bc[i]) + } + return ans - 1 } diff --git a/leetcode/1601-1700/1653.Minimum-Deletions-to-Make-String-Balanced/Solution_test.go b/leetcode/1601-1700/1653.Minimum-Deletions-to-Make-String-Balanced/Solution_test.go index 14ff50eb4..ef1de19a1 100644 --- a/leetcode/1601-1700/1653.Minimum-Deletions-to-Make-String-Balanced/Solution_test.go +++ b/leetcode/1601-1700/1653.Minimum-Deletions-to-Make-String-Balanced/Solution_test.go @@ -10,12 +10,11 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + expect int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "aababbab", 2}, + {"TestCase2", "bbaaaaabb", 2}, } // 开始测试 @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }