StringOps
SRM 80 · 2002-04-15 · by lbackstrom
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
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
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
" where is a sequence of non-space characters. "REPLACE " where and are both sequences of non-space characters.
{"but","do","perform","each","oopp"}
{"DELETE op","DELETE op"}
Returns: { "but", "do", "perform", "each", "" }
{"isisisisisisiss"}
{"REPLACE is i","DELETE i"}
Returns: { "s" }
{"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.
{"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.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.
Submissions are judged against all 62 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class StringOps with a public method vector<string> perform(vector<string> text, vector<string> ops) · 62 test cases · 2 s / 256 MB per case