forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdonghyeon95.java
More file actions
57 lines (49 loc) · 1.33 KB
/
donghyeon95.java
File metadata and controls
57 lines (49 loc) · 1.33 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
46
47
48
49
50
51
52
53
54
55
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Solution {
/*
* @param strs: a list of strings
* @return: encodes a list of strings to a single string.
*/
String cDel = "&";
String sDel = ";";
// Encodes a list of strings to a single string
public String encode(List<String> strs) {
if (strs.isEmpty()) return null;
StringBuilder result = new StringBuilder();
for (int i =0; i<strs.size(); i++) {
String str = strs.get(i);
StringBuilder temp = new StringBuilder();
for (char c : str.toCharArray()) {
temp.append((int) c).append(cDel);
}
result.append(temp);
if (i != strs.size()-1) result.append(sDel);
}
return result.toString();
}
// Decodes a single string to a list of strings
public List<String> decode(String str) {
if (str==null)
return new ArrayList<>();
List<String> result = new ArrayList<>();
String[] strs = str.split(sDel, -1);
for (String s : strs) {
if (s.isEmpty()) {
result.add("");
continue;
}
String[] chars = s.split(cDel);
String decoded = Arrays.stream(chars)
.filter(sr -> !sr.isEmpty())
.mapToInt(Integer::parseInt)
.mapToObj(ascii -> (char) ascii)
.map(String::valueOf)
.collect(Collectors.joining());
result.add(decoded);
}
return result;
}
}