File tree Expand file tree Collapse file tree 3 files changed +54
-2
lines changed
solution/2400-2499/2434.Using a Robot to Print the Lexicographically Smallest String Expand file tree Collapse file tree 3 files changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -240,7 +240,24 @@ func robotWithString(s string) string {
240
240
### ** TypeScript**
241
241
242
242
``` 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
+ };
244
261
```
245
262
246
263
### ** ...**
Original file line number Diff line number Diff line change @@ -221,7 +221,24 @@ func robotWithString(s string) string {
221
221
### ** TypeScript**
222
222
223
223
``` 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
+ };
225
242
```
226
243
227
244
### ** ...**
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments