THIS PROBLEM WAS TAKEN FROM THE SEMIFINALS OF THE TOPCODER INVITATIONAL
TOURNAMENT
PROBLEM STATEMENT
One of the problems that TopCoder has run into is how to parse a String[] from
the command line. To do this, we've adopted the following convention for a
String representation of a String[] (quotes are used for clarity only):
"{<element1>, <element2>, ... , <elementN>}", where the input String begins
with a beginning curly brace, each String element in the input String (except
the last one) is followed by a comma and a space, and the last one is followed
by an end curly brace. However, there are a couple of tricks:
* each individual String element may contain commas or curly braces (or both).
In each case, an escape character ('\') will precede commas and curly braces
that are actually *within* the String element.
* a backslash ('\') does not necessarily mean the following character needs to
be escaped. If it does not precede a comma or curly brace, it is to be
considered part of the String element.
* an empty String element is a valid parameter, and is indicated by no
character preceding a delimiting comma and a space (see examples below).
* "{}" returns an array with an empty String element {""}.
Additionally, there are certain cases where the input String is invalid that
you will need to catch. Those situations are:
* if the input string doesn't begin with an open curly brace and end with a
closed curly brace
* if any curly brace inside the opening and closing curly braces are not
escaped.
* if any comma that serves as a delimiter fails to have a space after it
In cases such as these, return a String[] consisting of exactly one element:
"INVALID"
Your task is to create a class ArgParser that includes a method parse. This
method will take a String representation of a String[], and return the
corresponding String[].
DEFINITION
Class Name: ArgParser
Method Name: parse
Parameters: String
Returns: String[]
Method signature (be sure your method is public): String[] parse(String input);
TopCoder will ensure that the input string will only contain characters A-Z,
a-z, 0-9, '{', '}', spaces (' '), commas (',') and backslashes ('\'). The
String should have a length between 0 and 50, inclusive.
EXAMPLES
"{a, b, c}" -> {"a", "b", "c"} (3 items)
"{a\,b, c}" -> {"a,b", "c"} (2 items)
"{, , a, }" -> {"", "", "a", ""} (4 items)
"{\\, \,\, }" -> {"\, ,, "} (1 item)
"{\ , \,, }" -> {"\ ", ",", ""} (3 items)
"{}" -> {""} (1 item)
|