Skip to content

Commit 66cbceb

Browse files
committed
feat: add typescript solution to lc problem: No.2434
No.2434.Using a Robot to Print the Lexicographically Smallest String
1 parent 1f0b418 commit 66cbceb

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

solution/2400-2499/2434.Using a Robot to Print the Lexicographically Smallest String/README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,24 @@ func robotWithString(s string) string {
240240
### **TypeScript**
241241

242242
```ts
243-
243+
function robotWithString(s: string): string {
244+
let cnt = new Array(128).fill(0);
245+
for (let c of s) cnt[c.charCodeAt(0)] += 1;
246+
let min_index = 'a'.charCodeAt(0);
247+
let ans = [];
248+
let stack = [];
249+
for (let c of s) {
250+
cnt[c.charCodeAt(0)] -= 1;
251+
while (min_index <= 'z'.charCodeAt(0) && cnt[min_index] == 0) {
252+
min_index += 1;
253+
}
254+
stack.push(c);
255+
while (stack.length > 0 && stack[stack.length - 1].charCodeAt(0) <= min_index) {
256+
ans.push(stack.pop());
257+
}
258+
}
259+
return ans.join('');
260+
};
244261
```
245262

246263
### **...**

solution/2400-2499/2434.Using a Robot to Print the Lexicographically Smallest String/README_EN.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,24 @@ func robotWithString(s string) string {
221221
### **TypeScript**
222222

223223
```ts
224-
224+
function robotWithString(s: string): string {
225+
let cnt = new Array(128).fill(0);
226+
for (let c of s) cnt[c.charCodeAt(0)] += 1;
227+
let min_index = 'a'.charCodeAt(0);
228+
let ans = [];
229+
let stack = [];
230+
for (let c of s) {
231+
cnt[c.charCodeAt(0)] -= 1;
232+
while (min_index <= 'z'.charCodeAt(0) && cnt[min_index] == 0) {
233+
min_index += 1;
234+
}
235+
stack.push(c);
236+
while (stack.length > 0 && stack[stack.length - 1].charCodeAt(0) <= min_index) {
237+
ans.push(stack.pop());
238+
}
239+
}
240+
return ans.join('');
241+
};
225242
```
226243

227244
### **...**
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function robotWithString(s: string): string {
2+
let cnt = new Array(128).fill(0);
3+
for (let c of s) cnt[c.charCodeAt(0)] += 1;
4+
let min_index = 'a'.charCodeAt(0);
5+
let ans = [];
6+
let stack = [];
7+
for (let c of s) {
8+
cnt[c.charCodeAt(0)] -= 1;
9+
while (min_index <= 'z'.charCodeAt(0) && cnt[min_index] == 0) {
10+
min_index += 1;
11+
}
12+
stack.push(c);
13+
while (stack.length > 0 && stack[stack.length - 1].charCodeAt(0) <= min_index) {
14+
ans.push(stack.pop());
15+
}
16+
}
17+
return ans.join('');
18+
};

0 commit comments

Comments
 (0)