Skip to content

Commit 5caf65c

Browse files
committed
add encode and decode strings
1 parent d5db2f4 commit 5caf65c

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+

0 commit comments

Comments
ย (0)