Skip to content

Commit

Permalink
Add solution and test-cases for problem 3039
Browse files Browse the repository at this point in the history
  • Loading branch information
0xff-dev committed Jun 17, 2024
1 parent 1940a25 commit 5d19c00
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
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() {
}

0 comments on commit 5d19c00

Please sign in to comment.