Skip to content

Add solution and test-cases for problem 2434 #1226

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,46 @@
# [2434.Using a Robot to Print the Lexicographically Smallest String][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` and a robot that currently holds an empty string `t`. Apply one of the following operations until `t` and `t` **are both empty**:

- Remove the **first** character of a string `s` and give it to the robot. The robot will append this character to the string `t`.
- Remove the **last** character of a string `t` and give it to the robot. The robot will write this character on paper.

Return the lexicographically smallest string that can be written on the paper.

**Example 1:**

```
Input: a = "11", b = "1"
Output: "100"
Input: s = "zza"
Output: "azz"
Explanation: Let p denote the written string.
Initially p="", s="zza", t="".
Perform first operation three times p="", s="", t="zza".
Perform second operation three times p="azz", s="", t="".
```

## 题意
> ...

## 题解
**Example 2:**

### 思路1
> ...
Using a Robot to Print the Lexicographically Smallest String
```go
```
Input: s = "bac"
Output: "abc"
Explanation: Let p denote the written string.
Perform first operation twice p="", s="c", t="ba".
Perform second operation twice p="ab", s="c", t="".
Perform first operation p="ab", s="", t="c".
Perform second operation p="abc", s="", t="".
```

**Example 3:**

```
Input: s = "bdda"
Output: "addb"
Explanation: Let p denote the written string.
Initially p="", s="bdda", t="".
Perform first operation four times p="", s="", t="bdda".
Perform second operation four times p="addb", s="", t="".
```

## 结语

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

func Solution(x bool) bool {
return x
func Solution(s string) string {
cnt := make([]int, 26)
for _, c := range s {
cnt[c-'a']++
}

stack := []rune{}
res := []rune{}
minCharacter := 'a'
for _, c := range s {
stack = append(stack, c)
cnt[c-'a']--
for minCharacter != 'z' && cnt[minCharacter-'a'] == 0 {
minCharacter++
}
for len(stack) > 0 && stack[len(stack)-1] <= minCharacter {
res = append(res, stack[len(stack)-1])
stack = stack[:len(stack)-1]
}
}

return string(res)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ 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", "zza", "azz"},
{"TestCase2", "bac", "abc"},
{"TestCase3", "bdda", "addb"},
}

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

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

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