Problem Statement
PROBLEM STATEMENT
Create a method that takes two arguments, an initial string and a String[] of
substitute strings. The initial string is scanned from left to right.
Characters in the string are left unchanged until an ampersand '&' character is
detected. When an ampersand is detected, the following substitutions are made:
1. If a letter follows the ampersand, both the ampersand and the letter are
deleted.
2. If a digit character (0-9) follows the ampersand, that digit is used as an
index into the substitute String[]. The index is zero based. If there is an
entry for that index, the string at that index replaces the original ampersand
and digit. If there is no entry (for example, if the digit is an index not
contained in the String[]) then the original ampersand and digit are deleted.
Rules 1, 2, and 3 are then applied to the substituted string (see examples).
3. If the character following the ampersand is another ampersand, then the
following ampersand is resolved first using rules 1, 2, and 3. Then the
original ampersand is processed using rules 1, 2, and 3.
4. Finally, all trailing ampersands are removed from the string and the
resulting string is returned.
IMPORTANT
It is possible for combinations of substitutions and substitute strings to
cause the result string to infinitely grow. Other combinations may cause the
string never to change, yet the end of processing is never reached. The method
coded must detect these non-terminating situations and return the special
string "***" (three asteriks) instead. See the example below.
No substitution string may contain a digit character equal to its index in the
String[]. For example, given the four element subsitute String[] containing
["xx&0", "1ABC", "abc2", "3333"],
each entry is considered invalid. The 0 entry, contains the digit 0, the 1
entry contains the digit 1, and so on. TopCoder will check this.
DEFINITION
Class Name: Ampersands
Method Name: resolve
Parameters: String, String[]
Returns: String
Method signature (be sure your method is public): String resolve(String
initial, String[] substitutions);
TopCoder will ensure the validity of the inputs. Inputs are valid if all of
the following criteria are met:
- The allowable characters in all strings in this problem are: A-Z, a-z, 0-9,
'&'
- All strings in this problem are from zero to four characters in length,
inclusive.
- substitutions may contain zero to four strings, inclusive.
- no substitution string may contain a digit character equal to its index in
the String[]
EXAMPLES
"xy&0", ["abc"] ==> "xyabc" ("&0" is replaced with "abc")
"&&&1", ["abc", "0&&&"] ==> "bc"
The first ampersand is temporarily skipped. The second ampersand is skipped as
well. The third ampersand is followed by the digit 1, and the ampersand and the
'1' are replaced by the string "0&&&" resulting in "&&0&&&". The second
ampersand, previously skipped, can now be processed. The second ampersand is
followed by the digit 0, so "&0" is replaced by the string "abc" resulting in
"&abc&&&". Now the first ampersand can be processed. Since the first ampersand
is now followed by the letter 'a', the first ampersand and the letter are
deleted resulting in "bc&&&". The ampersand following the character 'c' is
skipped and likewise, the next ampersand, and the last ampersand. The end of
the string is reached. The trailing ampersands are deleted and the final result
of "bc" is returned.
"&0", ["xy&1", "ab&0"] ==> "***"
&0 is replaced by xy&1. When the &1 in the substituted string is resolved,
another "ab&0" will be substituted. When ab&0 is resolved, "xy&1" will be
substituted for the "&0". This substitution sequence would continue
indefinitely. The method detects that this substitution sequencne would never
end and returns the string "***".
"&1x0" ["abc", "xy&&"] ==> "xyabc"
"&000" ["&&&1", "2222", "&3&3", "AbCd"] ==> "bCdAbCd22200"
"&0", ["&1", "&2", "&3", "&0"} ==> "***"
"&000", ["&&&1", "2222", "3333", "AbC&"] ==> "AbCAbCAbCAbC33332200"Definition
- Class:
- Ampersands
- Method:
- resolve
- Parameters:
- String, String[]
- Returns:
- String
- Method signature:
- String resolve(String param0, String[] param1)
- (be sure your method is public)
Constraints
Examples
No public sample cases were explicitly marked for this problem in the original data.