Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a2d88f7

Browse files
authored
Merge pull request #87 from willingtushar/patch-1
271-Encode-and-Decode-Strings.java
2 parents 1fb52ec + a588757 commit a2d88f7

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
}

0 commit comments

Comments
 (0)