File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ public class Solution {
2+
3+ public String encode (List <String > strs ) {
4+ StringBuilder encodedString = new StringBuilder ();
5+ for (String str : strs ){
6+ int length = str .length ();
7+ encodedString .append (length +"#" );
8+ encodedString .append (str );
9+ }
10+ return encodedString .toString ();
11+ }
12+
13+ public List <String > decode (String str ) {
14+ List <String > decodedStrings = new ArrayList ();
15+ for (int i =0 ;i <str .length ();i ++){
16+ String length = "" ;
17+ while (str .charAt (i ) != '#' ){
18+ length += str .charAt (i );
19+ i ++;
20+ }
21+ int wordLength = Integer .parseInt (length );
22+ i ++;
23+
24+ String word = "" ;
25+ for (int j =i ;j <wordLength +i ;j ++){
26+ word += str .charAt (j );
27+ }
28+ decodedStrings .add (word );
29+ i =i +wordLength -1 ;
30+ }
31+ return decodedStrings ;
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments