forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTonyKim9401.java
More file actions
32 lines (31 loc) · 967 Bytes
/
TonyKim9401.java
File metadata and controls
32 lines (31 loc) · 967 Bytes
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
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) {
// write your code here
StringBuilder sb = new StringBuilder();
for (String str : strs) {
sb.append(str.length()).append("#").append(str);
}
return sb.toString();
}
/*
* @param str: A string
* @return: decodes a single string to a list of strings
*/
public List<String> decode(String str) {
// write your code here
List<String> output = new ArrayList<>();
int i = 0;
while (i < str.length()) {
int idx = str.indexOf('#', i);
int length = Integer.parseInt(str.substring(i, idx));
String s = str.substring(idx + 1, idx + 1 + length);
output.add(s);
i = idx + 1 + length;
}
return output;
}
}