Skip to content

Commit 384c79c

Browse files
add: encode and decode
1 parent 16d2609 commit 384c79c

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class Solution {
5+
/*
6+
* @param strs: a list of strings
7+
* @return: encodes a list of strings to a single string.
8+
*/
9+
public String encode(List<String> strs) {
10+
List<String> temp = new ArrayList<>();
11+
12+
if(strs.size() == 0) return null;
13+
14+
for(String s : strs) {
15+
if(":".equals(s)) {
16+
temp.add("::");
17+
} else {
18+
temp.add(s);
19+
}
20+
}
21+
22+
return String.join(":;", temp);
23+
}
24+
25+
/*
26+
* @param str: A string
27+
* @return: decodes a single string to a list of strings
28+
*/
29+
public List<String> decode(String str) {
30+
List<String> temp = new ArrayList<>();
31+
32+
if(str == null) return new ArrayList<>();
33+
34+
// if(str.length() == 0) return new ArrayList<>();
35+
36+
for(String s : str.split(":;")) {
37+
if("::".equals(s)) {
38+
temp.add(":");
39+
} else {
40+
temp.add(s);
41+
}
42+
}
43+
return temp;
44+
}
45+
}

0 commit comments

Comments
 (0)