TopcoderArchiveVisit Topcoder
Archive
Algorithm problem

Serializer

Problem statement, definition, constraints, and public examples.

Problem Statement

When transferring data across a network, the data must be serialized as a sequence of bytes. In this problem, we want to write a custom serialization for a String[], which is easily readable to developers who might want to sniff packets for debugging purposes. Your task is to take a String[], and return a serialized version of it as a String according to the following rules.
1) You should use a '{' to mark the beginning of the serialization and a '}' to mark the end of the serialization.
2) Each element should be surrounded by a single set of double quotes.
3) All of the elements should then be concatenated with a single comma between each pair of adjacent elements.
Thus, the serialized version of the String[] will end up looking almost identical to the way it would appear in the source code of Java, C++, or C#. So, if you had a String with 2 elements: "hello" and "world" (quotes for clarity only), you would return the string:
{"hello","world"}

Definition

Class:
Serializer
Method:
serializeStringArray
Parameters:
String[]
Returns:
String
Method signature:
String serializeStringArray(String[] array)
(be sure your method is public)

Notes

  • In reality, you would have to worry about having double quotes within strings, and would have to use escape characters. However, in this problem you needn't worry about this because neither backslashes ('\'), nor double quotes ('"') will appear in the input.
  • You would also have to worry about the String[] being null, and elements of the String[] being null, but in this problem, neither the String[], nor elements of the String[] will be null.
  • Note that the expected results displayed in the examples have double quotes around them and backslashes before double quotes within the return. This is for clarity only. The sequence \" means a single double quote, and the leading and trailing double quotes serve to signify the beginning and end of the result string.

Constraints

  • array will contain between 0 and 50 elements, inclusive.
  • Each element of array will contain between 0 and 50 characters, inclusive.
  • All characters in each element of array will be printable ASCII characters. In other words, they will have ASCII values between 32 and 126, inclusive (all of these characters can appear on a standard keyboard). The only exceptions to this rule are the backslash ('\') and the double quote ('"'), which will not be allowed.

Examples

  1. {"task=order","pid=33"}
    Returns: "{\"task=order\",\"pid=33\"}"
    The return value, without the extra backslashes and extra double quotes is: {"task=order","pid=33"}
  2. {"single quotes (') are allowed","double quotes are not"}
    Returns: "{\"single quotes (') are allowed\",\"double quotes are not\"}"
    Without backslashes: {"single quotes (') are allowed","double quotes are not"}
  3. {"45A","43B","C41","EE","F41","2520","9D8"}
    Returns: "{\"45A\",\"43B\",\"C41\",\"EE\",\"F41\",\"2520\",\"9D8\"}"
    Without backslashes: {"45A","43B","C41","EE","F41","2520","9D8"}
Back to all problems