Task
Write a program or function that, given a string and a list of strings, replaces each occurrence of an element of the string list contained in the string with the next element of the string list.
Example
The string is The cat wore a hat on my doormat so I gave it a pat
and the list is ["at","or","oo"]
, we observe that the following bracketed substrings are in the list of strings:
The c[at] w[or]e a h[at] on my d[oo]rm[at] so I gave it a p[at]
We replace each element simultaneously with the element after it. The element after "at"
in the list is "or"
, the element after "or"
is "oo"
, and the element after "oo"
is "at"
. We get
The c[or] w[oo]e a h[or] on my d[at]rm[or] so I gave it a p[or]
so the output is
The cor wooe a hor a on my datrmor so I gave it a por
Note that when we were finding substrings, we selected d[oo]rm[at]
instead of do[or]m[at]
. This is because the substrings cannot overlap, so when deciding between overlapping substrings we choose the one(s) which come first in the string. When that has ambiguity, we choose the one(s) which come first in the input list.
Input
Input can be taken in any reasonable format. The list can be newline-delimited, space-delimited, or delimited by any reasonable character.
Output
Output the string generated after the replacements
Rules
- You may assume that the input string and list consist only of printable ASCII (0x20 to 0x7E).
- You may assume that the strings in the input list do not contain your delimiter.
- You may assume that the input list has no repeated elements.
- None of the Standard Loopholes are allowed.
- Since this is code-golf, lowest score in bytes wins.
Testcases
String
List of Strings
Output
-----
"My friend John went to the park."
["j","J"," "]
"Myjfriendj ohnjwentjtojthejpark."
-----
"Monkey Tacos!"
["Monkey","Programming Puzzles"," "," & ","Tacos!","Code Golf", "Puzzles", "Dragons"]
"Programming Puzzles & Code Golf"
-----
"No matches this time :("
["Match","Duel","(No"]
"No matches this time :("
-----
"aaaaa"
["aa","b","a","c"]
"bbc"
-----
"aaaaa"
["b","a","c","aa"]
"ccccc"
-----
"pencil"
["enc","pen","nci","cil"]
"ncienc"
-----
"Seriously, a lowercase rot one?"
["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
"Sfsjpvtmz, b mpxfsdbtf spu pof?"