Skip to content

Commit ffba06c

Browse files
committed
Add solution and test-cases for problem 2914
1 parent ffaca5d commit ffba06c

File tree

3 files changed

+58
-22
lines changed

3 files changed

+58
-22
lines changed

leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/README.md

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,44 @@
11
# [2914.Minimum Number of Changes to Make Binary String Beautiful][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
You are given a **0-indexed** binary string `s` having an even length.
5+
6+
A string is **beautiful** if it's possible to partition it into one or more substrings such that:
7+
8+
- Each substring has an **even length**.
9+
- Each substring contains **only** `1`'s or **only** `0`'s.
10+
11+
You can change any character in `s` to `0` or `1`.
12+
13+
Return the **minimum** number of changes required to make the string `s` beautiful.
714

815
**Example 1:**
916

1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: s = "1001"
19+
Output: 2
20+
Explanation: We change s[1] to 1 and s[3] to 0 to get string "1100".
21+
It can be seen that the string "1100" is beautiful because we can partition it into "11|00".
22+
It can be proven that 2 is the minimum number of changes needed to make the string beautiful.
1323
```
1424

15-
## 题意
16-
> ...
25+
**Example 2:**
1726

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Minimum Number of Changes to Make Binary String Beautiful
23-
```go
2427
```
28+
Input: s = "10"
29+
Output: 1
30+
Explanation: We change s[1] to 1 to get string "11".
31+
It can be seen that the string "11" is beautiful because we can partition it into "11".
32+
It can be proven that 1 is the minimum number of changes needed to make the string beautiful.
33+
```
34+
35+
**Example 3:**
2536

37+
```
38+
Input: s = "0000"
39+
Output: 0
40+
Explanation: We don't need to make any changes as the string "0000" is beautiful already.
41+
```
2642

2743
## 结语
2844

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) int {
4+
ans := 0
5+
cur := s[0]
6+
c := 1
7+
for i := 1; i < len(s); i++ {
8+
if s[i] == cur {
9+
c = 1 - c
10+
continue
11+
}
12+
if c == 0 {
13+
c = 1
14+
cur = s[i]
15+
continue
16+
}
17+
ans++
18+
i++
19+
if i != len(s) {
20+
cur = s[i]
21+
c = 1
22+
}
23+
}
24+
return ans
525
}

leetcode/2901-3000/2914.Minimum-Number-of-Changes-to-Make-Binary-String-Beautiful/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "1001", 2},
17+
{"TestCase2", "10", 1},
18+
{"TestCase3", "0000", 0},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)