forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoungSeok-Choi.java
More file actions
45 lines (37 loc) · 1.07 KB
/
YoungSeok-Choi.java
File metadata and controls
45 lines (37 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.ArrayList;
import java.util.List;
public class Solution {
/*
* @param strs: a list of strings
* @return: encodes a list of strings to a single string.
*/
public String encode(List<String> strs) {
List<String> temp = new ArrayList<>();
if(strs.size() == 0) return null;
for(String s : strs) {
if(":".equals(s)) {
temp.add("::");
} else {
temp.add(s);
}
}
return String.join(":;", temp);
}
/*
* @param str: A string
* @return: decodes a single string to a list of strings
*/
public List<String> decode(String str) {
List<String> temp = new ArrayList<>();
if(str == null) return new ArrayList<>();
// if(str.length() == 0) return new ArrayList<>();
for(String s : str.split(":;")) {
if("::".equals(s)) {
temp.add(":");
} else {
temp.add(s);
}
}
return temp;
}
}