File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {string } s
3
+ * @return {string }
4
+ */
5
+ const robotWithString = function ( s ) {
6
+ const stk = [ ]
7
+ const freq = Array ( 26 ) . fill ( 0 )
8
+ const a = 'a' . charCodeAt ( 0 )
9
+ for ( const ch of s ) {
10
+ freq [ ch . charCodeAt ( 0 ) - a ] ++
11
+ }
12
+
13
+ let res = ''
14
+
15
+ for ( const ch of s ) {
16
+ stk . push ( ch )
17
+ freq [ ch . charCodeAt ( 0 ) - a ] --
18
+ while ( stk . length && stk [ stk . length - 1 ] <= helper ( freq ) ) {
19
+ const e = stk . pop ( )
20
+ res += e
21
+ }
22
+ }
23
+
24
+ while ( stk . length ) {
25
+ res += stk . pop ( )
26
+ }
27
+
28
+ return res
29
+
30
+ function helper ( arr ) {
31
+ const a = 'a' . charCodeAt ( 0 )
32
+ for ( let i = 0 ; i < 26 ; i ++ ) {
33
+ if ( arr [ i ] !== 0 ) return String . fromCharCode ( a + i )
34
+ }
35
+
36
+ return ''
37
+ }
38
+ }
39
+
40
+
41
+ // another
1
42
2
43
const ord = ( c ) => c . charCodeAt ( ) ;
3
44
const char = ( ascii ) => String . fromCharCode ( ascii ) ;
You can’t perform that action at this time.
0 commit comments