File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ์๊ฐ ๋ณต์ก๋: O(n) - n์ ๋ชจ๋ ๋ฌธ์์ด์ ์ด ๊ธธ์ด. ๊ฐ ๋ฌธ์๋ฅผ ํ ๋ฒ์ฉ๋ง ์ฒ๋ฆฌํจ
3+ * ๊ณต๊ฐ ๋ณต์ก๋: O(n) - ์ธ์ฝ๋ฉ/๋์ฝ๋ฉ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ๋ ๋ฐ ์๋ณธ ๋ฐ์ดํฐ ํฌ๊ธฐ๋งํผ ๊ณต๊ฐ ํ์
4+ */
5+ var encode = function ( strs ) {
6+ let text = "" ;
7+ for ( let str of strs ) {
8+ // ๊ฐ ๋ฌธ์์ด ์์ ๊ธธ์ด์ ๊ตฌ๋ถ์(:)๋ฅผ ๋ถ์ฌ์ ์ ์ฅ
9+ // ์: ["abc", "de"] -> "3:abc2:de"
10+ text += `${ str . length } :${ str } ` ;
11+ }
12+ return text ;
13+ } ;
14+
15+ var decode = function ( s ) {
16+ const result = [ ] ;
17+ let start = 0 ;
18+
19+ while ( start < s . length ) {
20+ // ๊ฐ์ฅ ์ฒซ๋ฒ์งธ์ ๋ฑ์ฅํ๋ ์ฝ๋ก ์์น๋ฅผ ์ฐพ์ ๊ธธ์ด ์ ๋ณด ์ถ์ถ
21+ const mid = s . indexOf ( ":" , start ) ;
22+ const length = parseInt ( s . substring ( start , mid ) ) ;
23+
24+ // ๊ธธ์ด ์ ๋ณด๋ฅผ ์ด์ฉํด ์๋ ๋ฌธ์์ด ์ถ์ถํ์ฌ ๊ฒฐ๊ณผ ๋ฐฐ์ด์ ์ถ๊ฐ
25+ result . push ( s . substring ( mid + 1 , mid + 1 + length ) ) ;
26+
27+ // ๋ค์ ๋ฌธ์์ด์ ์์ ์์น๋ก ์ด๋
28+ start = mid + 1 + length ;
29+ }
30+
31+ return result ;
32+ } ;
You canโt perform that action at this time.
0 commit comments