Repository navigation
Expand file tree
/
Copy pathEncodeAndDecodeString.java
More file actions
62 lines (58 loc) · 1.75 KB
/
Copy pathEncodeAndDecodeString.java
File metadata and controls
62 lines (58 loc) · 1.75 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
56
57
58
59
60
61
62
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
class Solution {
// Encode and Decode Strings
// Design an algorithm to encode a list of string
// to a string. The encoded string is then sent over the network
// and is decodec back to the original list of strings.
// please implement encode and decode functions.
//
// Example :
// Input :["Hello", "World"]
// Output: 5#Hello5#World
// Explanation : The encoded string is "5#Hello5#World", where 5 is the lenght
// of
// "Hello " and "World"
//
// Input: [""]
// Output: "0#"
// Explanation: The encodes string is "0#" for an empty string
//
// Input: ["a", "b", "c"]
// Output: "1#a1#b1#c"
// Explanation: The encoded string is "1#a1#b1#c", wherei1 is then lenght of
// each string.
public static String encodeString(String[] strs) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < strs.length; i++) {
sb.append(strs[i].length()).append("#").append(strs[i]);
}
return sb.toString();
}
public static List<String> decodeString(String strs) {
List<String> result = new ArrayList<>();
int i = 0;
while (i < strs.length()) {
int j = i;
while (strs.charAt(j) != '#') {
j++;
}
int length = strs.charAt(j - 1);
result.add(strs.substring(j + 1, j + 1 + length));
i = j + 1 + length;
}
return result;
}
public static String encodeAndDecodeStrings(String strs) {
String[] arrStrs = strs.split(" ");
return encodeString(arrStrs);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String strs = scanner.nextLine();
scanner.close();
String res = encodeAndDecodeStrings(strs);
System.out.println(res);
}
}