File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * [Problem]: [659] Encode and Decode Strings
3+ *
4+ * (https://www.lintcode.com/problem/659/)
5+ */
6+
7+ //시간복잡도 O(n)
8+ //공간복잡도 O(1)
9+
10+ function encode ( strs : string [ ] ) : string {
11+ return strs . join ( "😄" ) ;
12+ }
13+ function decode ( s : string ) : string [ ] {
14+ return s . split ( "😄" ) ;
15+ }
16+
17+ // 시간복잡도: O(n)
18+ // 공간복잡도: O(n)
19+ function encode ( strs : string [ ] ) : string {
20+ return strs . map ( ( str ) => `${ str . length } :${ str } ` ) . join ( "" ) ;
21+ }
22+
23+ // 시간복잡도: O(n)
24+ // 공간복잡도: O(n)
25+ function decode ( str : string ) : string [ ] {
26+ const result : string [ ] = [ ] ;
27+ let index = 0 ;
28+ while ( index < str . length ) {
29+ const separatorIndex = str . indexOf ( ":" , index ) ;
30+ const length = + str . slice ( index , separatorIndex ) ;
31+ const endIndex = separatorIndex + 1 + length ;
32+
33+ result . push ( str . slice ( separatorIndex + 1 , endIndex ) ) ;
34+ index = endIndex ;
35+ }
36+ return result ;
37+ }
You can’t perform that action at this time.
0 commit comments