Skip to content

Commit 5b12185

Browse files
committed
Encode and Decode Strings
1 parent 764f4a1 commit 5b12185

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
Time Complexity: O(n)
3+
Space Complexity: O(1)
4+
5+
non-ASCII 유니코드 문자를 사용함
6+
7+
To-Do : escaping 방법 학습하기
8+
*/
9+
public class Codec {
10+
11+
// Encodes a list of strings to a single string.
12+
public String encode(List<String> strs) {
13+
StringBuilder sb = new StringBuilder();
14+
for (int i = 0; i < strs.size(); i++) {
15+
if (i > 0) {
16+
sb.append('\u2764');
17+
}
18+
sb.append(strs.get(i));
19+
}
20+
21+
return sb.toString();
22+
}
23+
24+
// Decodes a single string to a list of strings.
25+
public List<String> decode(String s) {
26+
return new ArrayList<>(Arrays.asList(s.split("\u2764")));
27+
}
28+
}
29+
30+
// Your Codec object will be instantiated and called as such:
31+
// Codec codec = new Codec();
32+
// codec.decode(codec.encode(strs));

0 commit comments

Comments
 (0)