File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change 1+ var encode = function ( strs ) {
2+ // strs: string[]
3+ // return: string
4+ // write your code here
5+
6+ let encode_result = "" ;
7+
8+ for ( const encode_strs of strs )
9+ {
10+ const encode_strs_length = encode_strs . length ;
11+
12+ if ( encode_result != undefined )
13+ {
14+ encode_result = encode_result + encode_strs_length + "|" + encode_strs ;
15+ }
16+
17+ }
18+
19+ return encode_result ;
20+ } ;
21+
22+ var decode = function ( str ) {
23+ // str: string
24+ // return: string[]
25+ // write your code here
26+
27+ let results = [ ] ;
28+
29+ while ( str . length > 0 ) {
30+ let i = 0 ;
31+ while ( str [ i ] >= '0' && str [ i ] <= '9' ) {
32+ i ++ ;
33+ }
34+
35+ let decode_str_length = str . substring ( 0 , i ) ;
36+ let len = parseInt ( decode_str_length , 10 ) ;
37+
38+ let newStr = str . substring ( i + 1 ) ;
39+ let result = newStr . substr ( 0 , decode_str_length )
40+
41+ str = newStr . substring ( len ) ;
42+
43+ results . push ( result ) ;
44+ }
45+
46+ return results ;
47+ } ;
48+
49+ console . log ( decode ( encode ( [ "lint" , "code" , "love" , "you" ] ) ) ) ;
50+ console . log ( decode ( encode ( [ "we" , "say" , ":" , "yes" ] ) ) ) ;
51+
You can’t perform that action at this time.
0 commit comments