File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change
1
+ import java .util .ArrayList ;
2
+ import java .util .List ;
3
+
4
+ public class Solution {
5
+ /*
6
+ * @param strs: a list of strings
7
+ * @return: encodes a list of strings to a single string.
8
+ */
9
+ public String encode (List <String > strs ) {
10
+ List <String > temp = new ArrayList <>();
11
+
12
+ if (strs .size () == 0 ) return null ;
13
+
14
+ for (String s : strs ) {
15
+ if (":" .equals (s )) {
16
+ temp .add ("::" );
17
+ } else {
18
+ temp .add (s );
19
+ }
20
+ }
21
+
22
+ return String .join (":;" , temp );
23
+ }
24
+
25
+ /*
26
+ * @param str: A string
27
+ * @return: decodes a single string to a list of strings
28
+ */
29
+ public List <String > decode (String str ) {
30
+ List <String > temp = new ArrayList <>();
31
+
32
+ if (str == null ) return new ArrayList <>();
33
+
34
+ // if(str.length() == 0) return new ArrayList<>();
35
+
36
+ for (String s : str .split (":;" )) {
37
+ if ("::" .equals (s )) {
38
+ temp .add (":" );
39
+ } else {
40
+ temp .add (s );
41
+ }
42
+ }
43
+ return temp ;
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments