Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 1653 #944

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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},
}

// 开始测试
Expand All @@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading