Skip to content

Commit 6e5fc68

Browse files
authored
Merge pull request #909 from 0xff-dev/main
Add solution and test-cases for problem 3039
2 parents b2703f4 + 5d19c00 commit 6e5fc68

File tree

3 files changed

+54
-23
lines changed

3 files changed

+54
-23
lines changed

leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/README.md

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [3039.Apply Operations to Make String Empty][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 string `s`.
5+
6+
Consider performing the following operation until `s` becomes **empty**:
7+
8+
- For **every** alphabet character from `'a'` to `'z'`, remove the **first** occurrence of that character in `s` (if it exists).
9+
10+
For example, let initially `s = "aabcbbca"`. We do the following operations:
11+
12+
- Remove the underlined characters `s = "aabcbbca"`. The resulting string is `s = "abbca"`.
13+
- Remove the underlined characters `s = "abbca"`. The resulting string is `s = "ba"`.
14+
- Remove the underlined characters `s = "ba"`. The resulting string is `s = ""`.
15+
16+
Return the value of the string `s` right **before** applying the **last** operation. In the example above, answer is `"ba"`.
717

818
**Example 1:**
919

1020
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
21+
Input: s = "aabcbbca"
22+
Output: "ba"
23+
Explanation: Explained in the statement.
1324
```
1425

15-
## 题意
16-
> ...
17-
18-
## 题解
26+
**Example 2:**
1927

20-
### 思路1
21-
> ...
22-
Apply Operations to Make String Empty
23-
```go
2428
```
25-
29+
Input: s = "abcd"
30+
Output: "abcd"
31+
Explanation: We do the following operation:
32+
- Remove the underlined characters s = "abcd". The resulting string is s = "".
33+
The string just before the last operation is "abcd".
34+
```
2635

2736
## 结语
2837

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

3-
func Solution(x bool) bool {
4-
return x
3+
import (
4+
"sort"
5+
"strings"
6+
)
7+
8+
func Solution(s string) string {
9+
count := [26][2]int{}
10+
mc := 0
11+
for i, b := range s {
12+
count[b-'a'][0]++
13+
count[b-'a'][1] = i
14+
mc = max(mc, count[b-'a'][0])
15+
}
16+
indies := []int{}
17+
for i := 0; i < 26; i++ {
18+
if count[i][0] == mc {
19+
indies = append(indies, count[i][1])
20+
}
21+
}
22+
sort.Ints(indies)
23+
sb := strings.Builder{}
24+
for _, i := range indies {
25+
sb.WriteByte(s[i])
26+
}
27+
return sb.String()
528
}

leetcode/3001-3100/3039.Apply-Operations-to-Make-String-Empty/Solution_test.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,11 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs string
14+
expect string
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", "aabcbbca", "ba"},
17+
{"TestCase2", "abcd", "abcd"},
1918
}
2019

2120
// 开始测试
@@ -30,10 +29,10 @@ func TestSolution(t *testing.T) {
3029
}
3130
}
3231

33-
// 压力测试
32+
// 压力测试
3433
func BenchmarkSolution(b *testing.B) {
3534
}
3635

37-
// 使用案列
36+
// 使用案列
3837
func ExampleSolution() {
3938
}

0 commit comments

Comments
 (0)