Subsequence Substitution
Python 2, 88 bytes
def f(a,b,c,o=""):
for q in a:x=q==b[:1];o+=c[:x]or q;b=b[x:];c=c[x:]
print[o,a][c>'']
A function that takes in the three strings and outputs the result to STDOUT. The function simply does one pass over the string, taking the appropriate char and updating b,c
as we go.
For testing (after replacing the print
with return
):
S = """
<test cases here>
"""
for T in S.split("\n\n"):
A,B,C,D = T.split()
assert f(A,B,C) == D
Java 7, 141
I think there's some more I can do with this, but I've gotta run for now. It's just a simple iterate/replace, keeping an index in A and B.
char[]h(char[]a,char[]b,char[]c){char[]d=a.clone();int i=0,j=0,k=b.length;for(;i<a.length&j<k;i++)if(a[i]==b[j])d[i]=c[j++];return j==k?d:a;}
Whitespaced for your pleasure:
char[]h(char[]a,char[]b,char[]c){
char[]d=a.clone();
int i=0,j=0,k=b.length;
for(;i<a.length&j<k;i++)
if(a[i]==b[j])d[i]=c[j++];
return j==k?d:a;
}
Lua, 121 Bytes
Straightforward solution, gsub
allows us to iterate exactly once on each character and to replace them in a new instance of the string.
It takes input via 3 command-line argument and output a string to STDOUT.
a,b,c=...d=a:gsub(".",function(s)if b:find(s)then b=b:sub(2)x=c:sub(1,1)c=c:sub(2)return x end end)print(b~=''and a or d)
Ungolfed
a,b,c=... -- unpack the arguments into a, b and c
d=a:gsub(".",function(s)-- iterate over each character of the first argument
if b:find(s)then -- if the current character is in the set b
b=b:sub(2) -- remove it from b
x=c:sub(1,1) -- save the replacement character in x
c=c:sub(2) -- remove it from c
return x -- replace the current character with x
end
end)
print(b~='' -- if b is empty, we replaced all the character
and a or d) -- so output the result of gsub, else, output the first argument