File tree Expand file tree Collapse file tree 1 file changed +39
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * ๋ฌธ์์ด์ ๋ํ encode, decode ์๊ณ ๋ฆฌ์ฆ ๋์์ธ
3+ * ๋ฌธ์์ด์ ASCII 256 ๋ชจ๋ ํฌํจ ๊ฐ๋ฅ (๋ฌธ์๋ง ํฌํจํ ๊ฒ ์๋๋ฏ๋ก ํน์๋ฌธ์(:, ?) ๋ฑ๋ ์๊ณ ๋ฆฌ์ฆ ๋ด์์ ๊ณ ๋ คํด์ผ ํจ
4+ * */
5+
6+ import java .util .ArrayList ;
7+ import java .util .List ;
8+
9+ public class Solution {
10+
11+ public static String encode (List <String > strs ) {
12+ StringBuilder sb = new StringBuilder ();
13+ for (String str : strs ) {
14+ // : ๊ตฌ๋ถ์ ์์ ๋จ์ด์ ๊ธธ์ด ์ถ๊ฐ
15+ sb .append (str .length ()).append (":" ).append (str );
16+ }
17+ return sb .toString ();
18+ }
19+
20+ public static List <String > decode (String str ) {
21+ List <String > words = new ArrayList <>();
22+ int i = 0 ;
23+ while (i < str .length ()) {
24+ // ๊ตฌ๋ถ์ ๊ธฐ์ค ์ธ๋ฑ์ค
25+ int colonIdx = str .indexOf (':' , i );
26+ // ๋จ์ด์ ๊ธธ์ด ์ธ๋ฑ์ค
27+ int length = Integer .parseInt (str .substring (i , colonIdx ));
28+ // ๋จ์ด ์์
29+ int wordStart = colonIdx + 1 ;
30+ // ๋จ์ด์ ๋
31+ int wordEnd = wordStart + length ;
32+ words .add (str .substring (wordStart , wordEnd ));
33+ i = wordEnd ;
34+ }
35+ return words ;
36+ }
37+
38+ }
39+
You canโt perform that action at this time.
0 commit comments