Skip to content

Commit bb3dbcb

Browse files
committed
+ problem 2434
1 parent 7195a53 commit bb3dbcb

File tree

5 files changed

+195
-0
lines changed

5 files changed

+195
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 2434. Using a Robot to Print the Lexicographically Smallest String
2+
You are given a string `s` and a robot that currently holds an empty string `t`. Apply one of the following operations until `s` and `t` **are both empty**:
3+
4+
* Remove the **first** character of a string `s` and give it to the robot. The robot will append this character to the string `t`.
5+
* Remove the **last** character of a string `t` and give it to the robot. The robot will write this character on paper.
6+
7+
Return *the lexicographically smallest string that can be written on the paper*.
8+
9+
#### Example 1:
10+
<pre>
11+
<strong>Input:</strong> s = "zza"
12+
<strong>Output:</strong> "azz"
13+
<strong>Explanation:</strong> Let p denote the written string.
14+
Initially p="", s="zza", t="".
15+
Perform first operation three times p="", s="", t="zza".
16+
Perform second operation three times p="azz", s="", t="".
17+
</pre>
18+
19+
#### Example 2:
20+
<pre>
21+
<strong>Input:</strong> s = "bac"
22+
<strong>Output:</strong> "abc"
23+
<strong>Explanation:</strong> Let p denote the written string.
24+
Perform first operation twice p="", s="c", t="ba".
25+
Perform second operation twice p="ab", s="c", t="".
26+
Perform first operation p="ab", s="", t="c".
27+
Perform second operation p="abc", s="", t="".
28+
</pre>
29+
30+
#### Example 3:
31+
<pre>
32+
<strong>Input:</strong> s = "bdda"
33+
<strong>Output:</strong> "addb"
34+
<strong>Explanation:</strong> Let p denote the written string.
35+
Initially p="", s="bdda", t="".
36+
Perform first operation four times p="", s="", t="bdda".
37+
Perform second operation four times p="addb", s="", t="".
38+
</pre>
39+
40+
#### Constraints:
41+
* <code>1 <= s.length <= 10<sup>5</sup></code>
42+
* `s` consists of only English lowercase letters.
43+
44+
## Solutions (Rust)
45+
46+
### 1. Solution
47+
```Rust
48+
impl Solution {
49+
pub fn robot_with_string(s: String) -> String {
50+
let mut s = s.bytes().rev().collect::<Vec<_>>();
51+
let mut t = vec![];
52+
let mut p = vec![];
53+
let mut count = [0; 26];
54+
55+
for &ch in &s {
56+
count[(ch - b'a') as usize] += 1;
57+
}
58+
59+
while let Some(ch0) = s.pop() {
60+
count[(ch0 - b'a') as usize] -= 1;
61+
t.push(ch0);
62+
63+
while let Some(&ch1) = t.last() {
64+
if count.iter().take((ch1 - b'a') as usize).all(|&x| x == 0) {
65+
p.push(t.pop().unwrap());
66+
} else {
67+
break;
68+
}
69+
}
70+
}
71+
72+
while let Some(ch) = t.pop() {
73+
p.push(ch);
74+
}
75+
76+
String::from_utf8(p).unwrap()
77+
}
78+
}
79+
```
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 2434. 使用机器人打印字典序最小的字符串
2+
给你一个字符串 `s` 和一个机器人,机器人当前有一个空字符串 `t` 。执行以下操作之一,直到 `s``t` **都变成空字符串**
3+
4+
* 删除字符串 `s`**第一个** 字符,并将该字符给机器人。机器人把这个字符添加到 `t` 的尾部。
5+
* 删除字符串 `t`**最后一个** 字符,并将该字符给机器人。机器人将该字符写到纸上。
6+
7+
请你返回纸上能写出的字典序最小的字符串。
8+
9+
#### 示例 1:
10+
<pre>
11+
<strong>输入:</strong> s = "zza"
12+
<strong>输出:</strong> "azz"
13+
<strong>解释:</strong> 用 p 表示写出来的字符串。
14+
一开始,p="" ,s="zza" ,t="" 。
15+
执行第一个操作三次,得到 p="" ,s="" ,t="zza" 。
16+
执行第二个操作三次,得到 p="azz" ,s="" ,t="" 。
17+
</pre>
18+
19+
#### 示例 2:
20+
<pre>
21+
<strong>输入:</strong> s = "bac"
22+
<strong>输出:</strong> "abc"
23+
<strong>解释:</strong> 用 p 表示写出来的字符串。
24+
执行第一个操作两次,得到 p="" ,s="c" ,t="ba" 。
25+
执行第二个操作两次,得到 p="ab" ,s="c" ,t="" 。
26+
执行第一个操作,得到 p="ab" ,s="" ,t="c" 。
27+
执行第二个操作,得到 p="abc" ,s="" ,t="" 。
28+
</pre>
29+
30+
#### 示例 3:
31+
<pre>
32+
<strong>输入:</strong> s = "bdda"
33+
<strong>输出:</strong> "addb"
34+
<strong>Explanation:</strong> 用 p 表示写出来的字符串。
35+
一开始,p="" ,s="bdda" ,t="" 。
36+
执行第一个操作四次,得到 p="" ,s="" ,t="bdda" 。
37+
执行第二个操作四次,得到 p="addb" ,s="" ,t="" 。
38+
</pre>
39+
40+
#### 提示:
41+
* <code>1 <= s.length <= 10<sup>5</sup></code>
42+
* `s` 只包含小写英文字母。
43+
44+
## 题解 (Rust)
45+
46+
### 1. 题解
47+
```Rust
48+
impl Solution {
49+
pub fn robot_with_string(s: String) -> String {
50+
let mut s = s.bytes().rev().collect::<Vec<_>>();
51+
let mut t = vec![];
52+
let mut p = vec![];
53+
let mut count = [0; 26];
54+
55+
for &ch in &s {
56+
count[(ch - b'a') as usize] += 1;
57+
}
58+
59+
while let Some(ch0) = s.pop() {
60+
count[(ch0 - b'a') as usize] -= 1;
61+
t.push(ch0);
62+
63+
while let Some(&ch1) = t.last() {
64+
if count.iter().take((ch1 - b'a') as usize).all(|&x| x == 0) {
65+
p.push(t.pop().unwrap());
66+
} else {
67+
break;
68+
}
69+
}
70+
}
71+
72+
while let Some(ch) = t.pop() {
73+
p.push(ch);
74+
}
75+
76+
String::from_utf8(p).unwrap()
77+
}
78+
}
79+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
impl Solution {
2+
pub fn robot_with_string(s: String) -> String {
3+
let mut s = s.bytes().rev().collect::<Vec<_>>();
4+
let mut t = vec![];
5+
let mut p = vec![];
6+
let mut count = [0; 26];
7+
8+
for &ch in &s {
9+
count[(ch - b'a') as usize] += 1;
10+
}
11+
12+
while let Some(ch0) = s.pop() {
13+
count[(ch0 - b'a') as usize] -= 1;
14+
t.push(ch0);
15+
16+
while let Some(&ch1) = t.last() {
17+
if count.iter().take((ch1 - b'a') as usize).all(|&x| x == 0) {
18+
p.push(t.pop().unwrap());
19+
} else {
20+
break;
21+
}
22+
}
23+
}
24+
25+
while let Some(ch) = t.pop() {
26+
p.push(ch);
27+
}
28+
29+
String::from_utf8(p).unwrap()
30+
}
31+
}

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,7 @@
11091109
[2429][2429l]|[Minimize XOR][2429] |![rs]
11101110
[2432][2432l]|[The Employee That Worked on the Longest Task][2432] |![rs]
11111111
[2433][2433l]|[Find The Original Array of Prefix Xor][2433] |![rs]
1112+
[2434][2434l]|[Using a Robot to Print the Lexicographically Smallest String][2434] |![rs]
11121113
[2437][2437l]|[Number of Valid Clock Times][2437] |![py]
11131114
[2441][2441l]|[Largest Positive Integer That Exists With Its Negative][2441] |![rs]
11141115
[2442][2442l]|[Count Number of Distinct Integers After Reverse Operations][2442] |![py]
@@ -2259,6 +2260,7 @@
22592260
[2429]:Problemset/2429-Minimize%20XOR/README.md#2429-minimize-xor
22602261
[2432]:Problemset/2432-The%20Employee%20That%20Worked%20on%20the%20Longest%20Task/README.md#2432-the-employee-that-worked-on-the-longest-task
22612262
[2433]:Problemset/2433-Find%20The%20Original%20Array%20of%20Prefix%20Xor/README.md#2433-find-the-original-array-of-prefix-xor
2263+
[2434]:Problemset/2434-Using%20a%20Robot%20to%20Print%20the%20Lexicographically%20Smallest%20String/README.md#2434-using-a-robot-to-print-the-lexicographically-smallest-string
22622264
[2437]:Problemset/2437-Number%20of%20Valid%20Clock%20Times/README.md#2437-number-of-valid-clock-times
22632265
[2441]:Problemset/2441-Largest%20Positive%20Integer%20That%20Exists%20With%20Its%20Negative/README.md#2441-largest-positive-integer-that-exists-with-its-negative
22642266
[2442]:Problemset/2442-Count%20Number%20of%20Distinct%20Integers%20After%20Reverse%20Operations/README.md#2442-count-number-of-distinct-integers-after-reverse-operations
@@ -3414,6 +3416,7 @@
34143416
[2429l]:https://leetcode.com/problems/minimize-xor/
34153417
[2432l]:https://leetcode.com/problems/the-employee-that-worked-on-the-longest-task/
34163418
[2433l]:https://leetcode.com/problems/find-the-original-array-of-prefix-xor/
3419+
[2434l]:https://leetcode.com/problems/using-a-robot-to-print-the-lexicographically-smallest-string/
34173420
[2437l]:https://leetcode.com/problems/number-of-valid-clock-times/
34183421
[2441l]:https://leetcode.com/problems/largest-positive-integer-that-exists-with-its-negative/
34193422
[2442l]:https://leetcode.com/problems/count-number-of-distinct-integers-after-reverse-operations/

README_CN.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,7 @@
11091109
[2429][2429l]|[最小 XOR][2429] |![rs]
11101110
[2432][2432l]|[处理用时最长的那个任务的员工][2432] |![rs]
11111111
[2433][2433l]|[找出前缀异或的原始数组][2433] |![rs]
1112+
[2434][2434l]|[使用机器人打印字典序最小的字符串][2434] |![rs]
11121113
[2437][2437l]|[有效时间的数目][2437] |![py]
11131114
[2441][2441l]|[与对应负数同时存在的最大正整数][2441] |![rs]
11141115
[2442][2442l]|[反转之后不同整数的数目][2442] |![py]
@@ -2259,6 +2260,7 @@
22592260
[2429]:Problemset/2429-Minimize%20XOR/README_CN.md#2429-最小-xor
22602261
[2432]:Problemset/2432-The%20Employee%20That%20Worked%20on%20the%20Longest%20Task/README_CN.md#2432-处理用时最长的那个任务的员工
22612262
[2433]:Problemset/2433-Find%20The%20Original%20Array%20of%20Prefix%20Xor/README_CN.md#2433-找出前缀异或的原始数组
2263+
[2434]:Problemset/2434-Using%20a%20Robot%20to%20Print%20the%20Lexicographically%20Smallest%20String/README_CN.md#2434-使用机器人打印字典序最小的字符串
22622264
[2437]:Problemset/2437-Number%20of%20Valid%20Clock%20Times/README_CN.md#2437-有效时间的数目
22632265
[2441]:Problemset/2441-Largest%20Positive%20Integer%20That%20Exists%20With%20Its%20Negative/README_CN.md#2441-与对应负数同时存在的最大正整数
22642266
[2442]:Problemset/2442-Count%20Number%20of%20Distinct%20Integers%20After%20Reverse%20Operations/README_CN.md#2442-反转之后不同整数的数目
@@ -3414,6 +3416,7 @@
34143416
[2429l]:https://leetcode.cn/problems/minimize-xor/
34153417
[2432l]:https://leetcode.cn/problems/the-employee-that-worked-on-the-longest-task/
34163418
[2433l]:https://leetcode.cn/problems/find-the-original-array-of-prefix-xor/
3419+
[2434l]:https://leetcode.cn/problems/using-a-robot-to-print-the-lexicographically-smallest-string/
34173420
[2437l]:https://leetcode.cn/problems/number-of-valid-clock-times/
34183421
[2441l]:https://leetcode.cn/problems/largest-positive-integer-that-exists-with-its-negative/
34193422
[2442l]:https://leetcode.cn/problems/count-number-of-distinct-integers-after-reverse-operations/

0 commit comments

Comments
 (0)