how to handle the input if the encoded string loses part of the information

  • 0

    In the highest voted post, num of string + 'any one character (separator)'+ string is used to encode the string list. And the decoding will work well in the ideal situation, but what if any one of the separator, or the number loses during transmission? How to make sure the decode() will not crash under these circumstances? This kind of real cases could be served as a follow-up during interview, but I find it kind of difficult because the encoded string could contain any 256 ASCII character.
    Any idea on improving the decode code?

     public List<String> decode(String s) {
            List<String> rst = new ArrayList<String>();
            int i = 0;
            while(i < s.length()) {
                int slash = s.indexOf('/', i);
                int size = Integer.valueOf(s.substring(i, slash));
                rst.add(s.substring(slash + 1, slash + size + 1));
                i = slash + size + 1;
            return rst;

Log in to reply

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