-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncodingDecoding.java
More file actions
executable file
·113 lines (104 loc) · 3.82 KB
/
Copy pathEncodingDecoding.java
File metadata and controls
executable file
·113 lines (104 loc) · 3.82 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package facebook;
import java.util.ArrayList;
import java.util.List;
/*
* Rule
* // ENCODE
* // e.g., sunny => 5/sunny
* // DECODE
* // s.indexOf('/',i), i=0 = > slashIdx => 1
* // s.substring(i,slashIdx) => "5"
* // s.substring(slashIdx+1, slashIdx+1+5) => sunny
* Design an algorithm to encode a list of strings to a string.
* The encoded string is then sent over the network and is decoded back to the original list of strings.
Machine 1 (sender) has the function:
string encode(vector<string> strs) {
// ... your code
return encoded_string;
}
Machine 2 (receiver) has the function:
vector<string> decode(string s) {
//... your code
return strs;
}
So Machine 1 does:
string encoded_string = encode(strs);
and Machine 2 does:
vector<string> strs2 = decode(encoded_string);
strs2 in Machine 2 should be the same as strs in Machine 1.
Implement the encode and decode methods.
Note:
The string may contain any possible characters out of 256 valid ascii characters.
Your algorithm should be generalized enough to work on any possible characters.
Do not use class member/global/static variables to store states.
Your encode and decode algorithms should be stateless.
Do not rely on any library method such as eval or serialize methods.
You should implement your own encode/decode algorithm.
*
http://buttercola.blogspot.com/2015/09/leetcode-encode-and-decode-strings.html
* */
public class EncodingDecoding {
/*encode
* //Zero: null check
// First :endcode 256 character ASCII
// Rule: (string length)+("/")+(string itself)
// e.g., sunny => 5/sunny
* */
public String encode(List<String> strs){
//Zero: null check
if (strs == null || strs.size() == 0)
return "";
// First :endcode 256 character ASCII
// Rule: (string length)+("/")+(string itself)
// e.g., sunny => 5/sunny
StringBuilder sb = new StringBuilder();
for (String s: strs){
sb.append(s.length()).append("/").append(s);
}
return sb.toString();
}
/*decode
* // Zero: null check
* // First: decode
// Rule: find the slash index and get the length from before and get substring from after
// indexOf(int ch, int fromIndex): starting the search at the specified index - fromIndex
// get the substring length - Integer.parseInt(s.substring(i, slashIdx))
// add the substring - s.substring(slashIdx + 1, slashIdx + size + 1)
// update the next starting point - slashIdx + size + 1
* */
public List<String> decode(String s){
List<String> res = new ArrayList<String>();
// Zero: null check
if (s == null || s.length() == 0)
return res;
// First: decode
// Rule: find the slash index and get the length from before and get substring from after
// indexOf(int ch, int fromIndex): starting the search at the specified index - fromIndex
// get the substring length - Integer.parseInt(s.substring(i, slashIdx))
// add the substring - s.substring(slashIdx + 1, slashIdx + size + 1)
// update the next starting point - slashIdx + size + 1
int i = 0;
while (i < s.length()){
int slashIdx = s.indexOf('/', i);// index of / most close to front
int size = Integer.parseInt(s.substring(i, slashIdx));
res.add(s.substring(slashIdx + 1, slashIdx + size + 1));
i = slashIdx + size + 1;
}
return res;
}
public static void main(String[] args){
List<String> list = new ArrayList<String>();
list.add("weihung");
list.add("liu");
list.add("is");
list.add("No.");
list.add("1");
list.add("awesome");
list.add("man");
list.add("/:");
EncodingDecoding sol = new EncodingDecoding();
String res = sol.encode(list);
System.out.println("Encode: "+res);
System.out.println("Decode: "+sol.decode(res));
}
}