TopcoderArchiveVisit Topcoder
SRM15 Apr 2002
SRM 80

StringOps

Problem statement, definition, constraints, and public examples.

Problem Statement

When editing text, especially source code, it is often very useful to have a search and replace function that finds all occurrences of a given string and replaces them with some other string. In addition, there are also circumstances when it is useful to be able to simply delete all occurrences of a string.

Your method will take a String[], text, whose elements are to have replace or delete operations performed on them. It will also take a second String[], ops, which has a number of commands that tell what replace or delete operation to perform.

Each op will be in one of the following two forms:
"DELETE <word>" where <word> is a sequence of non-space characters.
"REPLACE <word1> <word2>" where <word1> and <word2> are both sequences of non-space characters.

For DELETE operations, every place in text where the sequence of characters <word> occurs, all of these characters must be deleted. For REPLACE operations, every instance of <word1> must be replaced with <word2>.

Your method should perform all of these operations and then return a String[] that is the same as the input (text) except it has had each of the operations in ops performed on every element.

Definition

Class:
StringOps
Method:
perform
Parameters:
String[], String[]
Returns:
String[]
Method signature:
String[] perform(String[] text, String[] ops)
(be sure your method is public)

Notes

  • If there are multiple ways to apply an operation, replace or delete occurrences that appear earlier in the string first. (see example 1)
  • Replace and delete operations are not recursive (i.e., if we are replacing "is" with "i" and text contains "issis", we only replace once to get "isi". We do not continue and replace "is" again). (see examples 2 and 4)
  • All operations are case sensitive.

Constraints

  • All elements of ops start with "DELETE" or "REPLACE"
  • There will be exactly one space after the operation ("DELETE" or "REPLACE") and for replace operations, there will be exactly one space between the two words.
  • All elements of text will be between 1 and 50 characters in length, inclusive.
  • All elements of ops will be less than or equal to 50 characters in length.
  • At no point during the performance of the operations will any Strings be greater than 200 characters in length.
  • ops will contain between 0 and 50 elements, inclusive.
  • text will contain between 0 and 50 elements, inclusive.
  • All characters of all elements of ops and text will be either letters ('a'-'z' and 'A'-'Z'), numbers ('0'-'9'), punctuation (',' and '.') or spaces.
  • Each element of ops will be in one of the following two forms: "DELETE <word>" where <word> is a sequence of non-space characters. "REPLACE <word1> <word2>" where <word1> and <word2> are both sequences of non-space characters.

Examples

  1. {"replace me","but not me"}
    {"REPLACE replace delete","DELETE delete"}
    Returns: { " me",  "but not me" }
    First, we replace "replace" with "delete" in the first element of text. The second element is unaffected because it does not contain any occurrences of "replace". This gives us {"delete me","but not me"}. The second operation then deletes "delete" leaving us with {" me","but not me"}. Note the leading space in the first element.
  2. {"Mississippi"}
    {"DELETE issi"}
    Returns: { "Mssippi" }
    There are multiple occurrences of "issi" in "Mississippi", so we start by deleting the first one, which starts at character 1. This gives "Mssippi". There are no more occurrences of "issi" in the string, so we are done.
  3. {"3.1415927"}
    {"REPLACE 14 114","REPLACE 14 4"}
    Returns: { "3.1415927" }
    After performing the first operation, "3.1415927" becomes "3.11415927". After performing the second operation, "3.11415927" becomes "3.1415927". Note that in both instances, we did not replace occurrences with an operation if those occurrences were created by the same operation.
  4. {"the quick brown fox jumps over a lazy dog","spaces    dont     do    anything"}
    {"REPLACE fox wolf","REPLACE jumps looks","DELETE over","REPLACE o e","REPLACE e 0"}
    Returns: { "th0 quick br0wn w0lf l00ks  a lazy d0g",  "spac0s    d0nt     d0    anything" }
  5. {"iisiss"}
    {"DELETE is"}
    Returns: { "is" }
Back to all problems