Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Codec {
- // Encodes a list of strings to a single string.
- public String encode(List<String> strs) {
- StringBuilder sb = new StringBuilder();
- for(String str : strs)
- {
- sb.append(str.length()+"#");
- sb.append(str);
- }
- return sb.toString();
- }
- // Decodes a single string to a list of strings.
- public List<String> decode(String s) {
- List<String> list = new ArrayList<>();
- while(s.length() > 0)
- {
- int k = 0, i = 0;
- while('0' <= s.charAt(i) && s.charAt(i) <= '9')
- {
- k = k * 10 + s.charAt(i) - '0';
- i++;
- }
- list.add(s.substring(i + 1, i + 1 + k));
- s = s.substring(i + 1 + k);
- }
- return list;
- }
- }
- // Your Codec object will be instantiated and called as such:
- // Codec codec = new Codec();
- // codec.decode(codec.encode(strs));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement