Subsequence Substitution

Most languages come with a built-in to search a string for all occurrences of a given substring and replace those with another. I don't know of any language that generalises this concept to (not necessarily contiguous) subsequences. So that's your task in this challenge.

The input will consist of three strings A, B and C, where B and C are guaranteed to be of the same length. If B appears as a subsequence in A it should be replaced with C. Here is a simple example:

A: abcdefghijklmnopqrstuvwxyz
B: ghost
C: 12345


It would be processed like this:

abcdefghijklmnopqrstuvwxyz
||      |   ||
abcdef12ijklmn3pqr45uvwxyz


If there are several ways to find B as a subsequence, you should greedily replace the left-most one:

A: abcdeedcba
C: BOB

Result:   BbcOeedcbB
and NOT:  BbcdeeOcbB


The same applies if B could be found in multiple disjoint places:

A: abcdeedcbaabcde
B: ed
C: 12

Result:   abcd1e2cbaabcde
and NOT:  abcd112cbaabc2e (or similar)


When B does not appear in A, you should output A unchanged.

Rules

As stated above, take three strings A, B and C as input and replace the left-most occurrence of B as a subsequence in A with C, if there is any.

You may write a program or function, taking input via STDIN (or closest alternative), command-line argument or function argument and outputting the result via STDOUT (or closest alternative), function return value or function (out) parameter.

You may take the three strings in any consistent order which you should specify in your answer. You may assume that B and C have the same length. All strings will only contain alphanumeric characters.

Standard rules apply.

Test Cases

Each test case is four lines: A, B, C followed by the result.

abcdefghijklmnopqrstuvwxyz
ghost
12345
abcdef12ijklmn3pqr45uvwxyz

abcdeedcba
BOB
BbcOeedcbB

abcdeedcbaabcde
ed
12
abcd1e2cbaabcde

121
121
aBc
aBc

abcde
acb
123
abcde

ABC
ABCD
1234
ABC

012345678901234567890123456789
42
TT
0123T5678901T34567890123456789

edcbaedcbaedcbaedcba
abcde
12345
edcbaedcbaedcbaedcba

edcbaedcbaedcbaedcbaedcba
abcde
12345
edcb1edc2aed3bae4cba5dcba

daccdedca
ace
cra

aacbcbabcccaabcbabcaabbbbca
abaaaccbac
1223334444
aacbcbabcccaabcbabcaabbbbca

aacbcbabcccaabcbabcaabbbbcac
abaaaccbac
1223334444
1ac2cb2bccc33b3bab4aa4bbbc44


The Stack Snippet at the bottom of this post generates leaderboards from the answers a) as a list of shortest solution per language and b) as an overall leaderboard.

## Language Name, N bytes


where N is the size of your submission. If you improve your score, you can keep old scores in the headline, by striking them through. For instance:

## Ruby, <s>104</s> <s>101</s> 96 bytes


If there you want to include multiple numbers in your header (e.g. because your score is the sum of two files or you want to list interpreter flag penalties separately), make sure that the actual score is the last number in the header:

## Perl, 43 + 2 (-p flag) = 45 bytes


You can also make the language name a link which will then show up in the snippet:

## [><>](http://esolangs.org/wiki/Fish), 121 bytes


/* Configuration */

var QUESTION_ID = 77719; // Obtain this from the url
// It will be like http://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 8478; // This should be the user ID of the challenge author.

/* App */

return "http://api.stackexchange.com/2.2/questions/" +  QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}

}

jQuery.ajax({
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
data.items.forEach(function(a) {
});
comment_page = 1;
}
});
}

jQuery.ajax({
method: "get",
dataType: "jsonp",
crossDomain: true,
success: function (data) {
data.items.forEach(function(c) {
if (c.owner.user_id === OVERRIDE_USER)
});
else process();
}
});
}

var SCORE_REG = /<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;

function getAuthorName(a) {
return a.owner.display_name;
}

function process() {
var valid = [];

var body = a.body;
if(OVERRIDE_REG.test(c.body))
body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
});

var match = body.match(SCORE_REG);
if (match)
valid.push({
user: getAuthorName(a),
size: +match[2],
language: match[1],
});
else console.log(body);
});

valid.sort(function (a, b) {
var aB = a.size,
bB = b.size;
return aB - bB
});

var languages = {};
var place = 1;
var lastSize = null;
var lastPlace = 1;
valid.forEach(function (a) {
if (a.size != lastSize)
lastPlace = place;
lastSize = a.size;
++place;

.replace("{{NAME}}", a.user)
.replace("{{LANGUAGE}}", a.language)
.replace("{{SIZE}}", a.size)

var lang = a.language;
lang = jQuery('<a>'+lang+'</a>').text();

languages[lang] = languages[lang] || {lang: a.language, lang_raw: lang, user: a.user, size: a.size, link: a.link};
});

var langs = [];
for (var lang in languages)
if (languages.hasOwnProperty(lang))
langs.push(languages[lang]);

langs.sort(function (a, b) {
if (a.lang_raw.toLowerCase() > b.lang_raw.toLowerCase()) return 1;
if (a.lang_raw.toLowerCase() < b.lang_raw.toLowerCase()) return -1;
return 0;
});

for (var i = 0; i < langs.length; ++i)
{
var language = jQuery("#language-template").html();
var lang = langs[i];
language = language.replace("{{LANGUAGE}}", lang.lang)
.replace("{{NAME}}", lang.user)
.replace("{{SIZE}}", lang.size)
language = jQuery(language);
jQuery("#languages").append(language);
}

}
body { text-align: left !important}

width: 290px;
float: left;
}

#language-list {
width: 290px;
float: left;
}

font-weight: bold;
}

table td {
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="language-list">
<h2>Shortest Solution by Language</h2>
<table class="language-list">
<tr><td>Language</td><td>User</td><td>Score</td></tr>
<tbody id="languages">

</tbody>
</table>
</div>
<tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>

</tbody>
</table>
</div>
<table style="display: none">
</tbody>
</table>
<table style="display: none">
<tbody id="language-template">
</tbody>
</table>

• Would a list of single character strings be alright for input / output? – FryAmTheEggman Apr 13 '16 at 13:53
• @FryAmTheEggman Hm, the only consensus I can find is this which doesn't address lists of single-character strings as valid string representations. Might be worth making a meta post (especially because I think this also came up on xnor's latest challenge). I'm going to say no for now. – Martin Ender Apr 13 '16 at 13:58
• What about character arrays? This seems to imply they're allowed even if the language has a proper string type. – Dennis Apr 20 '16 at 18:22
• @Dennis Yeah, character arrays are fine, but singleton strings is like taking an array of integers as [[1], [2], [3]]. – Martin Ender Apr 20 '16 at 18:33
• OK, thanks for clearing that up. – Dennis Apr 20 '16 at 18:42

Jelly, 2322 21 bytes

='T€ŒpfṢ€$ḢṬœp³ż⁵$³Ḋ?


Try it online! Note that the last two test cases will run out of memory.

Verification

$head -n 5 test-cases abcdefghijklmnopqrstuvwxyz ghost 12345 abcdef12ijklmn3pqr45uvwxyz$ cat subseq-short
read p; read r; read o; echo $o; read timeout 1s jelly eun$1 "='T€ŒpfṢ€$ḢṬœp³ż⁵$³Ḋ?" "'$s'" "'$p'" "'$r'" (($?)) && echo '(killed)'
done < test-cases
$./subseq-short abcdef12ijklmn3pqr45uvwxyz abcdef12ijklmn3pqr45uvwxyz BbcOeedcbB BbcOeedcbB abcd1e2cbaabcde abcd1e2cbaabcde aBc aBc abcde abcde ABC ABC 0123T5678901T34567890123456789 0123T5678901T34567890123456789 edcbaedcbaedcbaedcba edcbaedcbaedcbaedcba edcb1edc2aed3bae4cba5dcba edcb1edc2aed3bae4cba5dcba dcrcdadca dcrcdadca aacbcbabcccaabcbabcaabbbbca (killed) 1ac2cb2bccc33b3bab4aa4bbbc44 (killed)  How it works ='T€ŒpfṢ€$ḢṬœp³ż⁵$³Ḋ? Main link. Arguments: string s, pattern p, replacement r =' Compare each character of s with each character of p. This yields a 2D list. Each row corresponds to a char in p. T€ Compute the truthy indices of each row, i.e., the indices of all occurrences of that char in s. Œp Compute the Cartesian product of the lists of indices.$              Combine the two links to the left into a monadic chain:
Ṣ€                 Sort each list of indices.
f                   Filter, removing all non-sorted lists of indices.
Ḣ             Head; take the first (sorted) list of indices.
Ṭ            Truth; generate a list with 1's at those indices.
œp³         Partition; split s at all 1's, removing those characters.
Ḋ?   If the partition has more than more than one element:
ż⁵$Zip the partition with r. ³ Else, return s.  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; }  • Whitespaced yeah, that's totally readable – cat Apr 14 '16 at 0:40 • Isn't it though? The main reason I add the multi-line indented version is to avoid horizontal scrolling, just so it can all be seen at once. Inline whitespace isn't as big a deal IMO ;) – Geobits Apr 14 '16 at 14:27 • [feature-request] even more whitespace – Alex A. Apr 14 '16 at 17:30 • @AlexA. Here ya go! – Geobits Apr 14 '16 at 19:34 • @Geobits Save a byte at the end if you do j<k?a:d – Xanderhall Dec 7 '16 at 13:48 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  Python 3, 127 bytes. Saved 16 bytes thanks to Katenkyo. Still working on this a bit, man was this nastier than I thought it would be. f=lambda a,b,c:a.replace(b[0],c[0],1)[:a.index(b[0])+1]+f(a[a.index(b[0])+1:],b[1:],c[1:])if b and all(x in a for x in b)else a  Explanation: Awww yeah, recursion. Test cases: assert f('abcdeedcba', 'ada', 'BOB') == 'BbcOeedcbB' assert f('abcdeedcbaabcde', 'ed', '12') == 'abcd1e2cbaabcde' assert f('012345678901234567890123456789', '42', 'TT') == '0123T5678901T34567890123456789' assert f('ABC', 'ABCD', '1234') == 'ABC'  • +1 for golfing 50 off, but keep going! This needs to beat my Java answer at the least ;) – Geobits Apr 13 '16 at 14:43 • @Geobits Yeah, I've never lost to Java before. This is my greatest shame. – Morgan Thrapp Apr 13 '16 at 14:43 • I'm not really versed in python but all(x in a for x in b) also checks that elements in b and a appears in the same order, or only if they are here? – Katenkyo Apr 13 '16 at 14:47 • @Katenkyo Only that they're all there, but the order gets taken care of by the slicing when we recurse. – Morgan Thrapp Apr 13 '16 at 14:48 • Ok, also, wouldn't return a.replace(b[0],c[0],1)[:l(b[0])+1]+f(a[l(b[0])+1:],b[1:],c[1:])if b and all(x in a for x in b)else a makes you save some bytes? – Katenkyo Apr 13 '16 at 14:49 Python 3.5, 87 bytes import re lambda s,p,r:re.sub('(.*?)'.join(p),'\g<%d>'.join(r)%(*range(1,len(r)),),s,1)  How it works • '(.*?)'.join(p) builds a search pattern that matches the subsequence to be replaced and anything between its elements. Since the quantifiers are lazy, each (.*?) will match as few characters as possible. For the pattern ghost, the constructed regex is g(.*?)h(.*?)o(.*?)s(.*?)t. • '\g<%d>'.join(r)%(*range(1,len(r)),) builds the replacement string, using string formatting. Each \g<n> refers to the nth captured group, just like \n would. For the replacement 12345, the constructed string is 1\g<1>2\g<2>3\g<3>4\g<4>5. • re.sub(...,...,s,1) performs at most one replacement in the string s. Pyth, 27 .xuXG.*HC,hSI#.nM*FxRcQ1zwQ  Test Suite The test suite omits the last two cases because they will run out of memory. The algorithm used here is to find all of the indices of each character in the second string in the first string, then find all possible orderings of those indices and take only the ones that are in sorted order. Then use the first one of these in sorted order as the list of indices in the first string to update with values from the third string. I feel like there should be something shorter than .nM*F... MATL, 33 bytes y!=[]0b"@n:!<@*fX<h5Mt?}.]]?iw(}x  Try it online! Explanation y! % Implicitly input first two strings. Duplicate the first and transpose = % Compare the two strings element-wise. Gives a 2D array with all combinations [] % Push empty array. Indices of matching elements will be appended to this 0 % Push a 0. This is the index of last character used up in first string b % Bubble up (rearrange elements in stack) to move 2D array to top " % For each column of that array (each char of the second string) @ % Push current column n:! % Transform into column array of consecutive values starting from 1 < % Compare with index of last character used up of first string @* % Push current column again. Multiply element-wise (logical AND) fX< % Find index of first matching character, or empty if there's none h % Append to array containing indices of matching elements 5Mt % Push index of matching character again. Duplicate ?} % If it's empty . % Break loop ] % End if ] % End for % The top of the stack now contains a copy of the index of last matching % character, or an empty array if there was no match ? % If non-empty: all characters were matched i % Input third string w % Swap top two elements in stack ( % Assign the characters of the third string to first string at found indices } % Else: the original string needs to be output x % Delete (partial) array of matching indices. Leave original string in stack % End if % Implicitly display (either modified string or original string)  JavaScript (ES6), 84 bytes (a,b,c)=>[...b].every((q,i)=>r[p=a.indexOf(q,p)]=~p++&&c[i],p=0,r=[...a])?r.join:a  Explanation / Test var solution = (a,b,c)=> [...b].every((q,i)=> r[p=a.indexOf(q,p)]=~p++&&c[i], p=0, r=[...a] )?r.join :a // Test var testCases = abcdefghijklmnopqrstuvwxyz ghost 12345 abcdef12ijklmn3pqr45uvwxyz abcdeedcba ada BOB BbcOeedcbB abcdeedcbaabcde ed 12 abcd1e2cbaabcde 121 121 aBc aBc abcde acb 123 abcde ABC ABCD 1234 ABC 012345678901234567890123456789 42 TT 0123T5678901T34567890123456789 edcbaedcbaedcbaedcba abcde 12345 edcbaedcbaedcbaedcba edcbaedcbaedcbaedcbaedcba abcde 12345 edcb1edc2aed3bae4cba5dcba daccdedca ace cra dcrcdadca aacbcbabcccaabcbabcaabbbbca abaaaccbac 1223334444 aacbcbabcccaabcbabcaabbbbca aacbcbabcccaabcbabcaabbbbcac abaaaccbac 1223334444 1ac2cb2bccc33b3bab4aa4bbbc44 .split\n\n.map(c=>c.split\n); document.write("<pre>"+testCases.map(c=>(r=solution(c[0],c[1],c[2]))+" : "+(r==c[3]?"PASS":"FAIL")).join\n); JavaScript (ES6), 84 76 bytes (a,b,c)=>a.replace(RegExp([...b].join(.*?)),c.replace(/\B/g,(_,i)=>'$'+i))


Because I was sure that this was a job for RegExp.

Edit: Saved 8 bytes thanks to @MartinBüttner♦.

A port of @KevinLau's Ruby answer took 82 bytes:

([...a],[...b],[...c])=>(d=a.map(e=>e==b[0]?c.shift(b.shift()):e),b[0]?a:d).join


I also tried a recursive RegExp solution but that took 90 bytes:

f=(a,[b,...d],[c,...e])=>b?a.replace(RegExp(b+'(.*'+d.join.*+'.*)'),(_,s)=>c+f(s,d,e)):a


Julia, 89 70 bytes

f(s,a,b,i=0)=(o=join(["$a "[i+1]!=c?c:b[i+=1]for c=s]);i<endof(a)?s:o)  Uses an index i to iterate through the pattern/replacement strings as we go. -19 bytes thanks to @Dennis! C, 98 bytes char*f(i,o,s,r)char*i,*o,*s,*r;{char*I=i,*O=o;for(;*i;++i,++o)*o=*i==*s?++s,*r++:*i;return*s?I:O;}  /* Expanded code */ char *f(i, o, s, r) char *i, *o, *s, *r; { char *I=i, *O=o; for (; *i; ++i,++o) *o = (*i==*s) ? (++s,*r++) : *i; return *s ? I : O; }  Arguments are: input string, output buffer, search string, replacement. After remembering the start of input and output, we walk the input, replacing and advancing the substitution whenever we hit one. At the end, if we've run out of substitutions, return the output buffer, else the unmodified input. /* Tests */ struct T { const char *input; const char *search; const char *replace; const char *expected; }; #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { int i; static const struct T test[] = { { "abcdefghijklmnopqrstuvwxyz", "ghost", "12345", "abcdef12ijklmn3pqr45uvwxyz"}, { "abcdeedcba", "ada", "BOB", "BbcOeedcbB"}, { "abcdeedcbaabcde", "ed", "12", "abcd1e2cbaabcde"}, { "121", "121", "aBc", "aBc"}, { "abcde", "acb", "123", "abcde"}, { "ABC", "ABCD", "1234", "ABC"}, { "012345678901234567890123456789", "42", "TT", "0123T5678901T34567890123456789"}, { "edcbaedcbaedcbaedcba", "abcde", "12345", "edcbaedcbaedcbaedcba"}, { "edcbaedcbaedcbaedcbaedcba", "abcde", "12345", "edcb1edc2aed3bae4cba5dcba"}, { "daccdedca", "ace", "cra", "dcrcdadca"}, { "aacbcbabcccaabcbabcaabbbbca", "abaaaccbac", "1223334444", "aacbcbabcccaabcbabcaabbbbca"}, { "aacbcbabcccaabcbabcaabbbbcac", "abaaaccbac", "1223334444", "1ac2cb2bccc33b3bab4aa4bbbc44" } }; for (i = 0; i < (sizeof test) / (sizeof test[0]); ++i) { const struct T *t = test+i; char *out = malloc(strlen(t->input)+1); char *result = f(t->input, out, t->search, t->replace); if (strcmp(t->expected, result)) printf("Failed test %d; result = \"%s\"\n", i, result); } return EXIT_SUCCESS; }  R, 76 bytes function(a,b,c){s=substr;for(x in 1:nchar(b)){a=sub(s(b,x,x),s(c,x,x),a)};a}  uses sub to replace first match Ungolfed function(a,b,c){ # function with 3 arguments as per description s=substr; # alias for substr (saves 1 byte) for(x in 1:nchar(b)){ # index 1 to number character in b a=sub(s(b,x,x),s(c,x,x),a)}; # replace first instance of b[x] in a # with c[x] and reassign to a a} # return a  C++, 204 bytes Golfed #include<iostream> #include<string> int main(){std::string a, b, c;std::cin>>a>>b>>c;int t=0;for(int x=0;x<b.length();x++){t=a.find(b[x],t);if(t!=-1){a.replace(t,1,c.substr(x,1));}}std::cout<<a;return 0;}  Ungolfed #include<iostream> #include<string> int main() { std::string a, b, c; std::cin>>a>>b>>c; int t = 0; for (int x=0;x<b.length();x++) { t = a.find(b[x], t); if (t != -1) { a.replace(t,1,c.substr(x, 1)); } } std::cout<<a; return 0; }  • I don't think you're using std quite enough to warrant using using namespace std;. Using std::cin, std::cout, and std::string will save 5 bytes since those seem to be the only uses of that namespace. – Value Ink Apr 14 '16 at 4:55 • @KevinLau Thanks! You're very correct, I thought of that, but didn't actually count to realise it would save chars. – Michelfrancis Bustillos Apr 14 '16 at 5:02 • Oh! One more thing, since it's important. After reading over your code again I realized you are greedily replacing the left-most occurrence of each letter within b in a, but the later letters have to be after the earlier letters as well. (Look at test case 3 and compare with your output, I think you'll find that your code would output abc21ed... when the expected output is abcd1e2...!) – Value Ink Apr 14 '16 at 5:12 • In ideone C++14 compiler input of "Adregffftd\nA23\nzac\n" above code 10 minutes ago, generate the output of "zdregffftd" instead of "Adregffftd" – RosLuP Dec 7 '16 at 16:12 Retina, 63 bytes .+$
$&¶;$&
+^(.)(.*¶)(.)([^;]+);(.*?)\1
$2$4$5$3;
A1
;

G2=.


Input is taken in order B, C, A, separated by linefeeds.

Try it online.

x@((a,b):c)#(d:e)|a==d,([],z)<-c#e=([],b:z)|0<1=(d:)<$>x#e x#y=(x,y) a!b=snd.(zip a b#)  I noticed the lack of a Haskell answer, and decided to fix that. This defines a ternary function ! with argument order pattern-replacement-string. Try it here. Explanation The auxiliary function # takes a list x of character pairs (pattern and replacement) and a string y. If the "pattern" characters in x form a subsequence of y, it returns the empty list and y with each pattern character replaced by its counterpart. Otherwise, it returns the pair (x,y). The function ! zips the pattern and replacement strings into x, applies # to x and the third string, and returns the second component of the result. x@((a,b):c)#(d:e) -- First case of #: both arguments nonempty. |a==d, -- If the pattern char matches the string's head, ([],z)<-c#e -- and the pattern's tail is a subsequence of the string's tail, =([],b:z) -- tack the replacement char to the recursion result. |0<1 -- Otherwise, =(d:)<$>x#e      -- recurse with the same pairs and tack string's head to result.
x#y=(x,y)          -- If either argument is empty, just pair them.


If the pattern is a subsequence of the string, the code runs in O(n) time, making one recursive pass through the string and greedily constructing the replacement in the process. However, if the pattern is not a subsequence, it runs in O(2n) time in the worst case. This is because at every position where the pattern and string have a matching character, the function calls itself to check whether the pattern is actually a subsequence, finds out it's not, and calls itself a second time to actually compute the result.

JavaScript (ES6), 100 95 bytes

(a,b,c)=>1?(t=[...a].map(e=>b[0]==e?(u=c[0],b=b.slice(1),c=c.slice(1),u):e).join,b==""?t:a):a


This is a valid JavaScript Lambda function. Outputs as function return. Takes in three arguments (a,b,c). Add f= at the beginning and invoke like f(arg1,arg2,arg3).

f=(a,b,c)=>1?(t=[...a].map(e=>b[0]==e?(u=c[0],b=b.slice(1),c=c.slice(1),u):e).join,b==""?t:a):a

console.log(f(prompt("Value for A"),prompt("Value for B"),prompt("Value for C")))

• Welcome to PPCG! Unnamed functions are generally acceptable, so you don't need the f= unless your function is recursive, but it doesn't look like it is. – Martin Ender Apr 14 '16 at 11:59
• @MartinBüttner Thanks! :) Updated my answer. – Arjun Apr 14 '16 at 13:20
• Unfortunately, this will fail if a doesn't contain the pattern. I'm also not sure returning an array of strings is acceptable. – Dennis Apr 16 '16 at 15:21
• @Dennis I have updated my solution. I think its correct now. Sorry for such late reply and updation. (I just noticed your comment, hence the delay) – Arjun May 4 '17 at 2:33
• @MartinEnder While I was going through all my solutions, I found that this one was incorrect. But, I have corrected it now; and it's five bytes shorter (since I had left many golfable places untouched; I was a novice golfer at that time; not that I am great one now, though :p). Sorry for posting a wrong solution. – Arjun May 4 '17 at 2:38

C (gcc), 676261 59 bytes

f(s,a,b)char*s,*a,*b;{*s==*a?*s=*b++,a++:1;*a&&f(s+1,a,b);}


Try it online!

Octave, 97 bytes

function A=U(A,B,C)t=0;for s=B if p=find(A(t+1:end)==s,1) D(t=p+t)=~0;else return;end;end;A(D)=C;


Iterate over the subsequence to replace; find first occurrence of first character, find next character in remaining string, repeat. The one interesting bit of this is:

D(t=p+t)=~0

D(     )      %// D is a logical mask of characters to replace in the input string
t=p+t       %// t is the current end of D
%// p is the location of the character to replace
%// update t and use as index to grow D
=~0   %// make it so, number 1


Since ideone is still not accepting functions with names other than '', I'll just leave a sample run here. Inputs are only shown for the first few test cases for brevity. key is the expected output, ans is the function output.

A = abcdefghijklmnopqrstuvwxyz
B = ghost
C = 12345
key = abcdef12ijklmn3pqr45uvwxyz
ans = abcdef12ijklmn3pqr45uvwxyz
A = abcdeedcba
C = BOB
key = BbcOeedcbB
ans = BbcOeedcbB
A = abcdeedcbaabcde
B = ed
C = 12
key = abcd1e2cbaabcde
ans = abcd1e2cbaabcde
key = aBc
ans = aBc
key = abcde
ans = abcde
key = ABC
ans = ABC
key = 0123T5678901T34567890123456789
ans = 0123T5678901T34567890123456789
key = edcbaedcbaedcbaedcba
ans = edcbaedcbaedcbaedcba
key = edcb1edc2aed3bae4cba5dcba
ans = edcb1edc2aed3bae4cba5dcba
key = aacbcbabcccaabcbabcaabbbbca
ans = aacbcbabcccaabcbabcaabbbbca
key = 1ac2cb2bccc33b3bab4aa4bbbc44
ans = 1ac2cb2bccc33b3bab4aa4bbbc44

• Those Octave assignments in unexpected places (D(t=...)) keep puzzling me :-) – Luis Mendo Apr 14 '16 at 23:03
• @LuisMendo haha... it's almost like... a stack! :) – beaker Apr 14 '16 at 23:14

Python 3, 123 Bytes

A different approach I wanted to share, which is a few bytes shorter. There are no rules against standard library/regex, right?

import re
j=''.join
m='(.*?)'
def f(A,B,C):
*r,l=(re.findall(m+m.join(B)+'(.*)',A)or[[A]])[0]
print(j(map(j,zip(r,C)))+l)


PS. This is my first golf. Let me know of any issues/improvements.

Pyth, 22 bytes

+E$Read the third line of input (pattern) and append ''. j"(.*?)" Join the result, separating by "(.*?)". E Read the third line of input (string). : 3 Match the string against the regex, as detailed above. J Save the returned array in J. e Extract the last element of J. This is an empty string for a successful match or the original string. | Logical OR; replace an empty string with the following: .iJQ Interleave J and the replacement. s Flatten the resulting array of strings.  Jelly, 23 bytes Ṭœpż⁵ 0ẋai1 ⁴='-;ç\ñ⁴P?  This is two bytes longer than my other Jelly answer, but it finishes instantly. Try it online! Verification  head -n 5 test-cases abcdefghijklmnopqrstuvwxyz ghost 12345 abcdef12ijklmn3pqr45uvwxyz  cat subseq-fast while read s; do read p; read r; read o; echo o; read timeout 10s jelly eun 1 "Ṭœpż⁵¶0ẋai1¶⁴='-;ç\ñ⁴P?" "'p'" "'s'" "'r'" ((?)) && echo '(killed)' done < test-cases  ./subseq-fast abcdef12ijklmn3pqr45uvwxyz abcdef12ijklmn3pqr45uvwxyz BbcOeedcbB BbcOeedcbB abcd1e2cbaabcde abcd1e2cbaabcde aBc aBc abcde abcde ABC ABC 0123T5678901T34567890123456789 0123T5678901T34567890123456789 edcbaedcbaedcbaedcba edcbaedcbaedcbaedcba edcb1edc2aed3bae4cba5dcba edcb1edc2aed3bae4cba5dcba dcrcdadca dcrcdadca aacbcbabcccaabcbabcaabbbbca aacbcbabcccaabcbabcaabbbbca 1ac2cb2bccc33b3bab4aa4bbbc44 1ac2cb2bccc33b3bab4aa4bbbc44  How it works ⁴='-;ç\ñ⁴P? Main link. Arguments: pattern p, string s, replacement r ⁴=' Compare each character of s with each character of p. This yields a 2D list. Each row corresponds to a char in p. -; Prepend -1 to the 2D list, yielding a ragged array. ç\ Cumulatively reduce the array by the second helper link. P? If the product of the resulting list is non-zero: ñ Call the first helper link with the list and s as arguments. ⁴ Else, return s. Ṭœpż⁵ First helper link. Arguments: L (list of indices), r (replacement) Ṭ Truth; generate a list with 1's at those indices. œp Partition; split s at all 1's, removing those characters. ż⁵ Zip the partition with r. 0ẋai1 Second helper link. Arguments: n (integer), B (list of Booleans) 0ẋ Generate a list of n zeroes. a Perform logical AND with B. This zeroes out the with n elements of B. i1 Compute the first index of 1.  CJam, 29 bytes l_ll.{2#_3<@+\)@>}s_2,>@@?  Java 7, 102 bytes void L(char[]s,char[]l,char[]r){for(int x=0,y=0;x<s.length&&y<l.length;x++)if(s[x]==l[y])s[x]=r[y++];}  Detailed try here // String, Lookup, Replacement void L(char[]s, char[]l, char[]r) { for(int x=0, y=0; x < s.length && y < l.length; x++) if(s[x] == l[y]) s[x] = r[y++]; }  Julia, 9390 86 bytes f(s,p,r)=(try s=join([match(Regex(join("^p$","(.*?)")),s).captures';[r...""]])end;s)


Having to test separately if the match was successful kinda destroys the score. A substitution would require casting to Base.SubstitutionString, which probably isn't worth it...

Clojure, 113 bytes

#(apply str((reduce(fn[[b c r]a](if(=(first b)a)[(rest b)(rest c)(conj r(first c))][b c(conj r a)]))[%2%3[]]%)2))


A basic reduce, not too happy about all those long first, rest and conj` function calls. Hoping to see a better approach.