-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathEncodeAndDecodeStrings.java
45 lines (39 loc) · 1.32 KB
/
EncodeAndDecodeStrings.java
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
package problems.leetcode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* https://leetcode.com/problems/encode-and-decode-strings/
*/
public class EncodeAndDecodeStrings {
// Encodes a list of strings to a single string.
public static String encode(List<String> strs) {
StringBuilder sb = new StringBuilder();
sb.append(strs.size()).append(",");
for (String s : strs) {
sb.append(s.length()).append(",").append(s);
}
return sb.toString();
}
// Decodes a single string to a list of strings.
public static List<String> decode(String encoded) {
int i = encoded.indexOf(',');
int count = Integer.parseInt(encoded.substring(0, i));
List<String> strs = new ArrayList<>(count);
i++;
while (i < encoded.length()) {
int j = encoded.indexOf(',', i);
int len = Integer.parseInt(encoded.substring(i, j));
j++;
strs.add(encoded.substring(j, j + len));
i = j + len;
}
return strs;
}
public static void main(String[] args) {
List<String> strs = Arrays.asList("ensar", "basri", "kahveci");
String encoded = encode(strs);
System.out.println(encoded);
System.out.println(decode(encoded));
}
}