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
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
" where is a sequence of non-space characters. "REPLACE " where and are both sequences of non-space characters.
Examples
{"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.
{"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" }
{"elite","hackers","talk","like","this","to","be","cool"}
{"REPLACE ite 33t","REPLACE el 1","REPLACE ck x","REPLACE er 0r","REPLACE s z","REPLACE o 0","REPLACE l 1","REPLACE a 4","REPLACE e 3","REPLACE c k","REPLACE i I"}
Returns: { "133t", "h4x0rz", "t41k", "1Ik3", "thIz", "t0", "b3", "k001" }
{"elite","hackers","talk","like","this","to","be","cool"}
{"REPLACE ite 33t","REPLACE el 1","REPLACE ck x","REPLACE er 0r","REPLACE s z","REPLACE o 0","REPLACE l 1","REPLACE a 4","REPLACE e 3","REPLACE c k","REPLACE i I"}
Returns: { "133t", "h4x0rz", "t41k", "1Ik3", "thIz", "t0", "b3", "k001" }
{"dont","recur"}
{"REPLACE cur ccur"}
Returns: { "dont", "reccur" }
{"delete","only","once","per","oopp"}
{"DELETE op"}
Returns: { "delete", "only", "once", "per", "op" }
{"ifdjsabvcdsajkytklh","fkfdjchdsa","fjdsakl","ewqupoc","random","stuff"}
{"REPLACE i fdacdahjkl","DELETE f","REPLACE k returnsMightBeLongerThan50Characters"}
Returns: { "dacdahjreturnsMightBeLongerThan50CharactersldjsabvcdsajreturnsMightBeLongerThan50CharactersytreturnsMightBeLongerThan50Characterslh", "returnsMightBeLongerThan50Charactersdjchdsa", "jdsareturnsMightBeLongerThan50Charactersl", "ewqupoc", "random", "stu" }
{"exponotential","growth","E"}
{"REPLACE E EE","REPLACE E EE","REPLACE E EE","REPLACE E EE","REPLACE E EE","REPLACE E EE"}
Returns: { "exponotential", "growth", "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE" }
{"EEEEEEEEEEEEEEEEEEEEEEAEEEEEEEEEEEEEEEEEEEEE"}
{"REPLACE EE E","REPLACE EE E","REPLACE EE E","REPLACE EE E","REPLACE EE E","REPLACE EE E","REPLACE EE E","REPLACE EE E","REPLACE EE E"}
Returns: { "EAE" }
{"iisiss"}
{"DELETE is"}
Returns: { "is" }
{"aabcbc"}
{"DELETE abc"}
Returns: { "abc" }
{"Mississippi","3.1415927","iisiss"}
{"DELETE issi","REPLACE 14 114","REPLACE 14 4","DELETE is"}
Returns: { "Mssippi", "3.1415927", "is" }
{"missiissi"}
{"REPLACE issi is"}
Returns: { "misis" }
{"3.14176897"}
{"REPLACE 1 114"}
Returns: { "3.114411476897" }
{"issssss","isssss"}
{"DELETE ss"}
Returns: { "i", "is" }
{"3.14176897"}
{"REPLACE 1 114"}
Returns: { "3.114411476897" }
{"eeeeee"}
{"REPLACE ee eeeeee"}
Returns: { "eeeeeeeeeeeeeeeeee" }
{"isisisisisisis"}
{"DELETE is"}
Returns: { "" }
{"ZAZBCDEREDZEDREDRedZED91RED1ZZZFDERZ"}
{"REPLACE RED I","REPLACE I RED","DELETE BC","DELETE E","REPLACE E I"}
Returns: { "ZAZDRDZDRDRedZD91RD1ZZZFDRZ" }
{"mississippi"}
{"REPLACE is ii"}
Returns: { "miisiisippi" }
{"abcbcbcc","abcabcabc","abccbccbcc","abcbccbbc"}
{"REPLACE bc bc","REPLACE bc b","REPLACE bc cb","DELETE b"}
Returns: { "ac", "aaa", "accc", "ac" }
{"3.14176897"}
{"REPLACE 1 114"}
Returns: { "3.114411476897" }
{"ABAB"}
{"REPLACE AB C"}
Returns: { "CC" }
{"iisiss"}
{"DELETE is"}
Returns: { "is" }
{"issis"}
{"REPLACE is i"}
Returns: { "isi" }
{"eeeebeebee"}
{"REPLACE e ee","REPLACE be ee","REPLACE eeee e"}
Returns: { "eeeeee" }
{"iss"}
{"REPLACE is i"}
Returns: { "is" }
{"abcdefabcdef"}
{"REPLACE abcdef xyz"}
Returns: { "xyzxyz" }
{"3.14176897"}
{"REPLACE 1 114"}
Returns: { "3.114411476897" }
{"iisiss"}
{"DELETE is"}
Returns: { "is" }
{"replaceme","butnotme"}
{"REPLACE replace delete","DELETE delete"}
Returns: { "me", "butnotme" }
{"testingstring","letsalsotrythis"}
{"REPLACE t tt","REPLACE l ll"}
Returns: { "ttesttingsttring", "llettsallsottrytthis" }
{"Ihopethisworks"}
{"REPLACE ho hohohohoho","DELETE works"}
Returns: { "Ihohohohohopethis" }
{"aabcbc"}
{"DELETE abc"}
Returns: { "abc" }
{"issss","isssss"}
{"DELETE ss"}
Returns: { "i", "is" }
{"a"}
{"REPLACE b c"}
Returns: { "a" }
{"iisiss"}
{"DELETE is"}
Returns: { "is" }
{"wowowordrdrdwowordrdwordwordblahblah"}
{"DELETE word"}
Returns: { "wowordrdwordblahblah" }
{"Mississippi","3.1415927","iisiss"}
{"DELETE issi","REPLACE 14 114","REPLACE 14 4","DELETE is"}
Returns: { "Mssippi", "3.1415927", "is" }
{"mississippi"}
{"REPLACE is ii"}
Returns: { "miisiisippi" }
{"looooooks","ihopethisworks"}
{"REPLACE o e","REPLACE ho hohohohoho","DELETE works"}
Returns: { "leeeeeeks", "ihepethiswerks" }
{"isisisis"}
{"DELETE is"}
Returns: { "" }
{"isisaa"}
{"DELETE isa"}
Returns: { "isa" }
{"abcbcbcbcc","abcabacabc","abcbcbcbc","abccbbccbbc","abcbcbccbcccba"}
{"REPLACE bc bc","REPLACE bc b","REPLACE bc cb","REPLACE bc bcb"}
Returns: { "abbbcbb", "ababacab", "abbbb", "acbbcbbbb", "abbcbbcbbcbba" }
{"isssss","issss"}
{"DELETE ss"}
Returns: { "is", "i" }
{"thequi1CkBr0wNfoxfojumfotheeeffoeeejfdeeefofoeeef"}
{"DELETE q","REPLACE f o","REPLACE fo le","REPLACE e e","DELETE e"}
Returns: { "thui1CkBr0wNooxoojumoothooojodooooo" }
{"aaaaikmn"}
{"REPLACE i ki"}
Returns: { "aaaakikmn" }
{"1"}
{"REPLACE 1 11"}
Returns: { "11" }
{"iiss"}
{"DELETE is"}
Returns: { "is" }
{"iii"}
{"REPLACE i s"}
Returns: { "sss" }
{"1"}
{"REPLACE 1 111"}
Returns: { "111" }
{"issss","isssss"}
{"DELETE ss"}
Returns: { "i", "is" }
{"R"}
{"DELETE R"}
Returns: { "" }
{"isisisisisisisis"}
{"DELETE is"}
Returns: { "" }
{"Mississipi"}
{"REPLACE iss i"}
Returns: { "Miiipi" }
{"mississippi"}
{"REPLACE issi iissi"}
Returns: { "miississippi" }
{"elite","hackers","talk","like","this","to","be","cool"}
{"REPLACE ite 33t","REPLACE el 1","REPLACE ck x","REPLACE er 0r","REPLACE s z","REPLACE o 0","REPLACE l 1","REPLACE a 4","REPLACE e 3","REPLACE c k","REPLACE i I"}
Returns: { "133t", "h4x0rz", "t41k", "1Ik3", "thIz", "t0", "b3", "k001" }
{"amquam"}
{"DELETE am"}
Returns: { "qu" }