TopCoder problem "Inserter" used in TCO '03 Qual. Round 1 (Division I Level Two)



Problem Statement

    

A common task in text editing is inserting one sequence of characters before another one. For example, in the String "10 10 bills", we might want to insert a dollar sign ('$') before the second occurrence of the number "10". Thus, the original string would become "10 $10 bills". You are to write a method which takes an original String and a series of insert instructions and performs the instructions, in the order they are given. You should return the modified String.

Instructions will be of the form "#<insert># #<before># <index>", where <insert> is the sequence of characters to be inserted, <before> is the sequence of characters before which to insert <insert>, and <index> is the index of the occurrence of <before> (starting from 1), before which to insert <insert>. In order to facilitate spaces in <insert> and <before>, both sequences will be surrounded by '#'. Thus the command "#insert this# #before this# 3" would insert "insert this" before the third occurrence of "before this".

If, for some command, there are less than <index> occurrences of <before>, then do nothing. For example, in the String "$100 $100", there is no third occurrence of "$", so a command that inserts before the third occurrence of "$" should be ignored.

 

Definition

    
Class:Inserter
Method:insert
Parameters:String[], String
Returns:String
Method signature:String insert(String[] commands, String original)
(be sure your method is public)
    
 

Notes

-All of the instructions are performed on the String as it changes.
-Searching for occurrences of <before> should be case sensitive.
-Note that the ith occurrence of <before> may overlap with the (i+1)st occurrence of <before>. See example 5.
 

Constraints

-original and each element of commands will have between 1 and 50 characters, inclusive.
-commands will contain between 0 and 50 elements, inclusive.
-Each character in the input will have ASCII value between 32 and 126, inclusive.
-Each element of commands will be formatted as described in the problem statement as "#<insert># #<before># <index>".
-The character '#' will not exist within either <before> or <insert>.
-Both <before> and <insert> will contain at least one character.
-Each <index> in command will be between 1 and 2^31-1, inclusive, with no extra leading zeros.
-The return will have at most 999 characters.
 

Examples

0)
    
{"#<B># #$# 2","#comes # #before this# 1"}
"$100 before this $300"
Returns: "$100 comes before this <B>$300"
The first command tells us to insert the sequence "<B>" before the second occurrence of "$". Doing this gives us: "$100 before this <B>$300"

The second command tells us to insert "comes " before "before this". Doing this gives us "$100 comes before this <B>$300"
1)
    
{"#,# # and# 1","#,# # and# 2","#,# # and# 3"}
"lions and tigers and bears"
Returns: "lions, and tigers, and bears"
Each command says to insert "," before " and". The first command says to insert before the 1st occurrence, the 2nd command says to insert before the 2nd occurrence, and the 3rd command says to insert before the 3rd occurrence (which does not exist).
2)
    
{"#import java.util.*; # #public class # 1"}
"public class Inserter"
Returns: "import java.util.*; public class Inserter"
3)
    
{"# # # # 1","# # # # 2","# # # # 3","# # # # 4","# # # # 5","# # # # 6"}
"add lots of spaces"
Returns: "add       lots of spaces"
4)
    
{"# # # # 6","# # # # 5","# # # # 4","# # # # 3","# # # # 2","# # # # 1"}
"order matters quite a bit"
Returns: "order  matters  quite  a  bit"
5)
    
{"#A# #..# 2","#A# #..# 5","#A# #..# 123456789"}
"...."
Returns: ".A..."

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=927

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4700&pm=927

Writer:

lars2520

Testers:

brett1479 , schveiguy

Problem categories:

String Manipulation, String Parsing