Skip to content

Add solution and test-cases for problem 3170 #1227

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@
# [3170.Lexicographically Minimum String After Removing Stars][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`. It may contain any number of `'*'` characters. Your task is to remove all `'*'` characters.

While there is a `'*'`, do the following operation:

- Delete the leftmost `'*'` and the **smallest** non-`'*'` character to its left. If there are several smallest characters, you can delete any of them.

Return the **lexicographically smallest** resulting string after removing all `'*'` characters.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
```
Input: s = "aaba*"

## 题意
> ...
Output: "aab"

## 题解
Explanation:

### 思路1
> ...
Lexicographically Minimum String After Removing Stars
```go
We should delete one of the 'a' characters with '*'. If we choose s[3], s becomes the lexicographically smallest.
```

**Example 2:**

```
Input: s = "abc"

Output: "abc"

Explanation:

There is no '*' in the string.
```

## 结语

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

func Solution(x bool) bool {
return x
func Solution(s string) string {
cnt := make([][]int, 26)
for i := range cnt {
cnt[i] = make([]int, 0)
}
arr := []rune(s)
for i, c := range arr {
if c != '*' {
cnt[c-'a'] = append(cnt[c-'a'], i)
} else {
for j := 0; j < 26; j++ {
if len(cnt[j]) > 0 {
last := len(cnt[j]) - 1
arr[cnt[j][last]] = '*'
cnt[j] = cnt[j][:last]
break
}
}
}
}
var ans []rune
for _, c := range arr {
if c != '*' {
ans = append(ans, c)
}
}
return string(ans)
}
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", "aaba*", "aab"},
{"TestCase2", "abc", "abc"},
}

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

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

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