File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
encode-and-decode-strings Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ public class Solution {
2
+ /*
3
+ * @param strs: a list of strings
4
+ * @return: encodes a list of strings to a single string.
5
+ */
6
+ public String encode (List <String > strs ) {
7
+ // write your code here
8
+ return strs .parallelStream ()
9
+ .map (s ->
10
+ s .chars ()
11
+ .parallel ()
12
+ .mapToObj (c -> new StringBuilder (String .format ("%02x" , c )))
13
+ .collect (
14
+ StringBuilder ::new ,
15
+ StringBuilder ::append ,
16
+ (a , b ) -> a .append (b )
17
+ )
18
+ ).collect (
19
+ StringBuilder ::new ,
20
+ (a , b ) -> a .append (b )
21
+ .append (' ' ),
22
+ (a , b ) -> a .append (b )
23
+ ).toString ();
24
+ }
25
+
26
+ /*
27
+ * @param str: A string
28
+ * @return: decodes a single string to a list of strings
29
+ */
30
+ public List <String > decode (String str ) {
31
+ // write your code here
32
+ final List <String > ans = new ArrayList <>();
33
+ final StringTokenizer st = new StringTokenizer (str );
34
+ while (st .hasMoreTokens ()) {
35
+ final String s = st .nextToken ();
36
+ final StringBuilder sb = new StringBuilder ();
37
+ for (int i = 0 ; i < s .length (); i += 2 ) {
38
+ sb .append ((char ) Integer .parseInt (s .substring (i , i + 2 ), 16 ));
39
+ }
40
+ ans .add (sb .toString ());
41
+ }
42
+ return ans ;
43
+ }
44
+ }
You can’t perform that action at this time.
0 commit comments