java solution by replacing chars


  • 0
    2
    public class Codec {
    
        // Encodes a list of strings to a single string.
        public String encode(List<String> strs) {
            StringBuilder sb = new StringBuilder();
            for (String s : strs) {
                sb.append("\0\0").append(s.replace("\0", "\0\1"));
            }
            return sb.toString();
        }
    
        // Decodes a single string to a list of strings.
        public List<String> decode(String s) {
            List<String> l = new ArrayList<String>();
            if (s.length() != 0) {
                for (String token : s.substring(2).split("\0\0", -1)) {
                    l.add(token.replace("\0\1", "\0").toString());
                }
            }
            return l;
        }
    }
    
    // Your Codec object will be instantiated and called as such:
    // Codec codec = new Codec();
    // codec.decode(codec.encode(strs));
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.