Skip to content

Commit 03a916e

Browse files
committed
Add solution and test-cases for problem 2434
1 parent 31756cd commit 03a916e

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/README.md

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,46 @@
11
# [2434.Using a Robot to Print the Lexicographically Smallest String][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` and a robot that currently holds an empty string `t`. Apply one of the following operations until `t` and `t` **are both empty**:
5+
6+
- Remove the **first** character of a string `s` and give it to the robot. The robot will append this character to the string `t`.
7+
- Remove the **last** character of a string `t` and give it to the robot. The robot will write this character on paper.
8+
9+
Return the lexicographically smallest string that can be written on the paper.
710

811
**Example 1:**
912

1013
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
14+
Input: s = "zza"
15+
Output: "azz"
16+
Explanation: Let p denote the written string.
17+
Initially p="", s="zza", t="".
18+
Perform first operation three times p="", s="", t="zza".
19+
Perform second operation three times p="azz", s="", t="".
1320
```
1421

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

20-
### 思路1
21-
> ...
22-
Using a Robot to Print the Lexicographically Smallest String
23-
```go
2424
```
25+
Input: s = "bac"
26+
Output: "abc"
27+
Explanation: Let p denote the written string.
28+
Perform first operation twice p="", s="c", t="ba".
29+
Perform second operation twice p="ab", s="c", t="".
30+
Perform first operation p="ab", s="", t="c".
31+
Perform second operation p="abc", s="", t="".
32+
```
33+
34+
**Example 3:**
2535

36+
```
37+
Input: s = "bdda"
38+
Output: "addb"
39+
Explanation: Let p denote the written string.
40+
Initially p="", s="bdda", t="".
41+
Perform first operation four times p="", s="", t="bdda".
42+
Perform second operation four times p="addb", s="", t="".
43+
```
2644

2745
## 结语
2846

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
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 _, c := range s {
6+
cnt[c-'a']++
7+
}
8+
9+
stack := []rune{}
10+
res := []rune{}
11+
minCharacter := 'a'
12+
for _, c := range s {
13+
stack = append(stack, c)
14+
cnt[c-'a']--
15+
for minCharacter != 'z' && cnt[minCharacter-'a'] == 0 {
16+
minCharacter++
17+
}
18+
for len(stack) > 0 && stack[len(stack)-1] <= minCharacter {
19+
res = append(res, stack[len(stack)-1])
20+
stack = stack[:len(stack)-1]
21+
}
22+
}
23+
24+
return string(res)
525
}

leetcode/2401-2500/2434.Using-a-Robot-to-Print-the-Lexicographically-Smallest-String/Solution_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ 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", "zza", "azz"},
17+
{"TestCase2", "bac", "abc"},
18+
{"TestCase3", "bdda", "addb"},
1919
}
2020

2121
// 开始测试
@@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33-
// 压力测试
33+
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
3535
}
3636

37-
// 使用案列
37+
// 使用案列
3838
func ExampleSolution() {
3939
}

0 commit comments

Comments
 (0)