Skip to content

Commit d1634f8

Browse files
committed
Add solution and test-cases for problem 3170
1 parent 31756cd commit d1634f8

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/README.md

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,37 @@
11
# [3170.Lexicographically Minimum String After Removing Stars][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`. It may contain any number of `'*'` characters. Your task is to remove all `'*'` characters.
5+
6+
While there is a `'*'`, do the following operation:
7+
8+
- Delete the leftmost `'*'` and the **smallest** non-`'*'` character to its left. If there are several smallest characters, you can delete any of them.
9+
10+
Return the **lexicographically smallest** resulting string after removing all `'*'` characters.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13-
```
15+
Input: s = "aaba*"
1416
15-
## 题意
16-
> ...
17+
Output: "aab"
1718
18-
## 题解
19+
Explanation:
1920
20-
### 思路1
21-
> ...
22-
Lexicographically Minimum String After Removing Stars
23-
```go
21+
We should delete one of the 'a' characters with '*'. If we choose s[3], s becomes the lexicographically smallest.
2422
```
2523

24+
**Example 2:**
25+
26+
```
27+
Input: s = "abc"
28+
29+
Output: "abc"
30+
31+
Explanation:
32+
33+
There is no '*' in the string.
34+
```
2635

2736
## 结语
2837

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(s string) string {
4+
cnt := make([][]int, 26)
5+
for i := range cnt {
6+
cnt[i] = make([]int, 0)
7+
}
8+
arr := []rune(s)
9+
for i, c := range arr {
10+
if c != '*' {
11+
cnt[c-'a'] = append(cnt[c-'a'], i)
12+
} else {
13+
for j := 0; j < 26; j++ {
14+
if len(cnt[j]) > 0 {
15+
last := len(cnt[j]) - 1
16+
arr[cnt[j][last]] = '*'
17+
cnt[j] = cnt[j][:last]
18+
break
19+
}
20+
}
21+
}
22+
}
23+
var ans []rune
24+
for _, c := range arr {
25+
if c != '*' {
26+
ans = append(ans, c)
27+
}
28+
}
29+
return string(ans)
530
}

leetcode/3101-3200/3170.Lexicographically-Minimum-String-After-Removing-Stars/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", "aaba*", "aab"},
17+
{"TestCase2", "abc", "abc"},
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)