Posts Deserialize (InterviewBit)
Post
Cancel

Deserialize (InterviewBit)

PROBLEM DESCRIPTION

You are given a string A which is a serialized string. You have to restore the original array of strings.

The string in the output array should only have lowercase english alphabets.

Serialization: Scan each element in a string, calculate its length and append it with a string and a element separator or deliminator (the deliminator is ~). We append the length of the string so that we know the length of each element.

For example, for a string ‘interviewbit’, its serialized version would be ‘interviewbit12~’.

SOLUTION

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class Solution {

    public String[] deserialize(String A) {

        String[] arr = A.split("~");

        int n = arr.length;
        String[] ans = new String[n];

        for(int i=0; i<n; i++){

            StringBuffer sb = new StringBuffer();

            int idx = 0;
            while(arr[i].charAt(idx) >= 'a' && arr[i].charAt(idx) <= 'z'){
                sb.append(String.valueOf(arr[i].charAt(idx)));
                idx++;
            }

            ans[i] = sb.toString();

        }

        return ans;

    }

}
This post is licensed under CC BY 4.0 by the author.