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 3039 #909

Merged
merged 1 commit into from
Jun 30, 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,37 @@
# [3039.Apply Operations to Make String Empty][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`.

Consider performing the following operation until `s` becomes **empty**:

- For **every** alphabet character from `'a'` to `'z'`, remove the **first** occurrence of that character in `s` (if it exists).

For example, let initially `s = "aabcbbca"`. We do the following operations:

- Remove the underlined characters `s = "aabcbbca"`. The resulting string is `s = "abbca"`.
- Remove the underlined characters `s = "abbca"`. The resulting string is `s = "ba"`.
- Remove the underlined characters `s = "ba"`. The resulting string is `s = ""`.

Return the value of the string `s` right **before** applying the **last** operation. In the example above, answer is `"ba"`.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "aabcbbca"
Output: "ba"
Explanation: Explained in the statement.
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Apply Operations to Make String Empty
```go
```

Input: s = "abcd"
Output: "abcd"
Explanation: We do the following operation:
- Remove the underlined characters s = "abcd". The resulting string is s = "".
The string just before the last operation is "abcd".
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
package Solution

func Solution(x bool) bool {
return x
import (
"sort"
"strings"
)

func Solution(s string) string {
count := [26][2]int{}
mc := 0
for i, b := range s {
count[b-'a'][0]++
count[b-'a'][1] = i
mc = max(mc, count[b-'a'][0])
}
indies := []int{}
for i := 0; i < 26; i++ {
if count[i][0] == mc {
indies = append(indies, count[i][1])
}
}
sort.Ints(indies)
sb := strings.Builder{}
for _, i := range indies {
sb.WriteByte(s[i])
}
return sb.String()
}
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 string
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", "aabcbbca", "ba"},
{"TestCase2", "abcd", "abcd"},
}

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

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

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