diff --git a/leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/README.md b/leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/README.md index f0661fed0..630af23da 100755 --- a/leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/README.md +++ b/leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/README.md @@ -1,28 +1,44 @@ # [2914.Minimum Number of Changes to Make Binary String Beautiful][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 **0-indexed** binary string `s` having an even length. + +A string is **beautiful** if it's possible to partition it into one or more substrings such that: + +- Each substring has an **even length**. +- Each substring contains **only** `1`'s or **only** `0`'s. + +You can change any character in `s` to `0` or `1`. + +Return the **minimum** number of changes required to make the string `s` beautiful. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "1001" +Output: 2 +Explanation: We change s[1] to 1 and s[3] to 0 to get string "1100". +It can be seen that the string "1100" is beautiful because we can partition it into "11|00". +It can be proven that 2 is the minimum number of changes needed to make the string beautiful. ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Minimum Number of Changes to Make Binary String Beautiful -```go ``` +Input: s = "10" +Output: 1 +Explanation: We change s[1] to 1 to get string "11". +It can be seen that the string "11" is beautiful because we can partition it into "11". +It can be proven that 1 is the minimum number of changes needed to make the string beautiful. +``` + +**Example 3:** +``` +Input: s = "0000" +Output: 0 +Explanation: We don't need to make any changes as the string "0000" is beautiful already. +``` ## 结语 diff --git a/leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/Solution.go b/leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/Solution.go index d115ccf5e..b7c4444b9 100644 --- a/leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/Solution.go +++ b/leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/Solution.go @@ -1,5 +1,25 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string) int { + ans := 0 + cur := s[0] + c := 1 + for i := 1; i < len(s); i++ { + if s[i] == cur { + c = 1 - c + continue + } + if c == 0 { + c = 1 + cur = s[i] + continue + } + ans++ + i++ + if i != len(s) { + cur = s[i] + c = 1 + } + } + return ans } diff --git a/leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/Solution_test.go b/leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/Solution_test.go index 14ff50eb4..02406c9f2 100644 --- a/leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/Solution_test.go +++ b/leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/Solution_test.go @@ -10,12 +10,12 @@ 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", "1001", 2}, + {"TestCase2", "10", 1}, + {"TestCase3", "0000", 0}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }