32
\$\begingroup\$

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
B: ada
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
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

Leaderboard

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.

To make sure that your answer shows up, please start your answer with a headline, using the following Markdown template:

## 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 ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 8478; // This should be the user ID of the challenge author.

/* App */

var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;

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

function commentUrl(index, answers) {
  return "http://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}

function getAnswers() {
  jQuery.ajax({
    url: answersUrl(answer_page++),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      answers.push.apply(answers, data.items);
      answers_hash = [];
      answer_ids = [];
      data.items.forEach(function(a) {
        a.comments = [];
        var id = +a.share_link.match(/\d+/);
        answer_ids.push(id);
        answers_hash[id] = a;
      });
      if (!data.has_more) more_answers = false;
      comment_page = 1;
      getComments();
    }
  });
}

function getComments() {
  jQuery.ajax({
    url: commentUrl(comment_page++, answer_ids),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      data.items.forEach(function(c) {
        if (c.owner.user_id === OVERRIDE_USER)
          answers_hash[c.post_id].comments.push(c);
      });
      if (data.has_more) getComments();
      else if (more_answers) getAnswers();
      else process();
    }
  });  
}

getAnswers();

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

var OVERRIDE_REG = /^Override\s*header:\s*/i;

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

function process() {
  var valid = [];
  
  answers.forEach(function(a) {
    var body = a.body;
    a.comments.forEach(function(c) {
      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],
        link: a.share_link,
      });
    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;
    
    var answer = jQuery("#answer-template").html();
    answer = answer.replace("{{PLACE}}", lastPlace + ".")
                   .replace("{{NAME}}", a.user)
                   .replace("{{LANGUAGE}}", a.language)
                   .replace("{{SIZE}}", a.size)
                   .replace("{{LINK}}", a.link);
    answer = jQuery(answer);
    jQuery("#answers").append(answer);

    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)
                       .replace("{{LINK}}", lang.link);
    language = jQuery(language);
    jQuery("#languages").append(language);
  }

}
body { text-align: left !important}

#answer-list {
  padding: 10px;
  width: 290px;
  float: left;
}

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

table thead {
  font-weight: bold;
}

table td {
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b">
<div id="language-list">
  <h2>Shortest Solution by Language</h2>
  <table class="language-list">
    <thead>
      <tr><td>Language</td><td>User</td><td>Score</td></tr>
    </thead>
    <tbody id="languages">

    </tbody>
  </table>
</div>
<div id="answer-list">
  <h2>Leaderboard</h2>
  <table class="answer-list">
    <thead>
      <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
    </thead>
    <tbody id="answers">

    </tbody>
  </table>
</div>
<table style="display: none">
  <tbody id="answer-template">
    <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>
<table style="display: none">
  <tbody id="language-template">
    <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>

\$\endgroup\$
5
  • \$\begingroup\$ Would a list of single character strings be alright for input / output? \$\endgroup\$ Commented Apr 13, 2016 at 13:53
  • \$\begingroup\$ @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. \$\endgroup\$ Commented Apr 13, 2016 at 13:58
  • \$\begingroup\$ What about character arrays? This seems to imply they're allowed even if the language has a proper string type. \$\endgroup\$
    – Dennis
    Commented Apr 20, 2016 at 18:22
  • \$\begingroup\$ @Dennis Yeah, character arrays are fine, but singleton strings is like taking an array of integers as [[1], [2], [3]]. \$\endgroup\$ Commented Apr 20, 2016 at 18:33
  • \$\begingroup\$ OK, thanks for clearing that up. \$\endgroup\$
    – Dennis
    Commented Apr 20, 2016 at 18:42

36 Answers 36

12
\$\begingroup\$

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
\$\endgroup\$
0
9
\$\begingroup\$

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;
}
\$\endgroup\$
5
  • \$\begingroup\$ Whitespaced yeah, that's totally readable \$\endgroup\$
    – cat
    Commented Apr 14, 2016 at 0:40
  • \$\begingroup\$ 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 ;) \$\endgroup\$
    – Geobits
    Commented Apr 14, 2016 at 14:27
  • \$\begingroup\$ [feature-request] even more whitespace \$\endgroup\$
    – Alex A.
    Commented Apr 14, 2016 at 17:30
  • \$\begingroup\$ @AlexA. Here ya go! \$\endgroup\$
    – Geobits
    Commented Apr 14, 2016 at 19:34
  • \$\begingroup\$ @Geobits Save a byte at the end if you do j<k?a:d \$\endgroup\$
    – Xanderhall
    Commented Dec 7, 2016 at 13:48
7
\$\begingroup\$

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
\$\endgroup\$
6
\$\begingroup\$

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'
\$\endgroup\$
7
  • \$\begingroup\$ +1 for golfing 50 off, but keep going! This needs to beat my Java answer at the least ;) \$\endgroup\$
    – Geobits
    Commented Apr 13, 2016 at 14:43
  • 7
    \$\begingroup\$ @Geobits Yeah, I've never lost to Java before. This is my greatest shame. \$\endgroup\$ Commented Apr 13, 2016 at 14:43
  • \$\begingroup\$ 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? \$\endgroup\$
    – Katenkyo
    Commented Apr 13, 2016 at 14:47
  • \$\begingroup\$ @Katenkyo Only that they're all there, but the order gets taken care of by the slicing when we recurse. \$\endgroup\$ Commented Apr 13, 2016 at 14:48
  • \$\begingroup\$ 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? \$\endgroup\$
    – Katenkyo
    Commented Apr 13, 2016 at 14:49
5
\$\begingroup\$

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)

repl.it to verify all test cases.

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.

\$\endgroup\$
0
4
\$\begingroup\$

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...

\$\endgroup\$
4
\$\begingroup\$

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)
\$\endgroup\$
0
3
\$\begingroup\$

Jelly, 23 22 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
while read s; do
        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.
\$\endgroup\$
3
\$\begingroup\$

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`);

\$\endgroup\$
3
\$\begingroup\$

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!

\$\endgroup\$
3
\$\begingroup\$

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
\$\endgroup\$
0
2
\$\begingroup\$

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;
}
\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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;
}
\$\endgroup\$
4
  • \$\begingroup\$ 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. \$\endgroup\$
    – Value Ink
    Commented Apr 14, 2016 at 4:55
  • \$\begingroup\$ @KevinLau Thanks! You're very correct, I thought of that, but didn't actually count to realise it would save chars. \$\endgroup\$ Commented Apr 14, 2016 at 5:02
  • \$\begingroup\$ 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...!) \$\endgroup\$
    – Value Ink
    Commented Apr 14, 2016 at 5:12
  • \$\begingroup\$ In ideone C++14 compiler input of "Adregffftd\nA23\nzac\n" above code 10 minutes ago, generate the output of "zdregffftd" instead of "Adregffftd" \$\endgroup\$
    – user58988
    Commented Dec 7, 2016 at 16:12
2
\$\begingroup\$

Retina, 63 bytes

.+$
$&¶;$&
+`^(.)(.*¶)(.)([^;]+);(.*?)\1
$2$4$5$3;
A1`
;

G2=`.

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

Try it online.

\$\endgroup\$
2
\$\begingroup\$

Haskell, 87 bytes

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.

\$\endgroup\$
2
\$\begingroup\$

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")))

\$\endgroup\$
5
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Apr 14, 2016 at 11:59
  • \$\begingroup\$ @MartinBüttner Thanks! :) Updated my answer. \$\endgroup\$
    – Arjun
    Commented Apr 14, 2016 at 13:20
  • \$\begingroup\$ Unfortunately, this will fail if a doesn't contain the pattern. I'm also not sure returning an array of strings is acceptable. \$\endgroup\$
    – Dennis
    Commented Apr 16, 2016 at 15:21
  • \$\begingroup\$ @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) \$\endgroup\$
    – Arjun
    Commented May 4, 2017 at 2:33
  • \$\begingroup\$ @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. \$\endgroup\$
    – Arjun
    Commented May 4, 2017 at 2:38
2
\$\begingroup\$

C (gcc), 67 62 61 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!

\$\endgroup\$
1
  • \$\begingroup\$ This fails if B does not appear in A. \$\endgroup\$
    – Shaggy
    Commented Oct 17 at 17:10
1
\$\begingroup\$

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
B = ada
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 = dcrcdadca
ans = dcrcdadca
key = aacbcbabcccaabcbabcaabbbbca
ans = aacbcbabcccaabcbabcaabbbbca
key = 1ac2cb2bccc33b3bab4aa4bbbc44
ans = 1ac2cb2bccc33b3bab4aa4bbbc44
\$\endgroup\$
2
  • \$\begingroup\$ Those Octave assignments in unexpected places (D(t=...)) keep puzzling me :-) \$\endgroup\$
    – Luis Mendo
    Commented Apr 14, 2016 at 23:03
  • 1
    \$\begingroup\$ @LuisMendo haha... it's almost like... a stack! :) \$\endgroup\$
    – beaker
    Commented Apr 14, 2016 at 23:14
1
\$\begingroup\$

Pyth, 22 bytes

|eJ:Ej"(.*?)"+E\$3s.iJ

Verify all test cases in the Pyth Compiler.

Background

We build a regex from the pattern by appending a $ and placing (.*?) between all characters. This regex will match the subsequence to be replaced and anything between its elements, and anything up to the end of the string.

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(.*?)$.

If the pattern matches the input, the builtin r<str><regex>3 will return an array containing the prematch (everything before the subsequence), all captured groups (everything in between and after the subsequence), and the postmatch (the empty string).

If the pattern does not match, the builtin will return a singleton array containing the original input.

How it works

|eJ:Ej"(.*?)"+E\$3s.iJQ  (implicit) Store the first line of input in Q.

             +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.
\$\endgroup\$
0
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

CJam, 29 bytes

l_ll.{2$#_3$<@+\)@>}s_2$,>@@?

Try it online! or verify all test cases.

\$\endgroup\$
1
\$\begingroup\$

Julia, 93 90 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...

Test run

julia> f(s,p,r)=(try s=join([match(Regex(join("^$p\$","(.*?)")),s).captures';[r...""]])end;s)
f (generic function with 1 method)

julia> f("aacbcbabcccaabcbabcaabbbbca","abaaaccbac","1223334444")
"aacbcbabcccaabcbabcaabbbbca"

julia> f("aacbcbabcccaabcbabcaabbbbcac","abaaaccbac","1223334444")
"1ac2cb2bccc33b3bab4aa4bbbc44"
\$\endgroup\$
1
\$\begingroup\$

Julia, 62 59 58 bytes

f(s,p,r)=(try s[[i=findnext(s,c,i+1)for c=p]'],i=r,0end;s)

I/O is in form of character arrays.

Verification

julia> f(s,p,r)=(try s[[i=findnext(s,c,i+1)for c=p]'],i=r,0end;s)
f (generic function with 2 methods)

julia> F(s,p,r)=join(f([s...],[p...],[r...])) # string/char array conversion
F (generic function with 1 method)

julia> F("aacbcbabcccaabcbabcaabbbbca","abaaaccbac","1223334444")
"aacbcbabcccaabcbabcaabbbbca"

julia> F("aacbcbabcccaabcbabcaabbbbcac","abaaaccbac","1223334444")
"1ac2cb2bccc33b3bab4aa4bbbc44"
\$\endgroup\$
1
\$\begingroup\$

PHP, 130 109 bytes

I´d still like it shorter; could save 3 bytes (""<) if B was guaranteed to not contain 0.

for($s=($a=$argv)[1];""<$c=$a[2][$i++];)if($p=strpos(_.$s,$c,$p+1))$s[$p-1]=$a[3][$k++];echo$k<$i-1?$a[1]:$s;

takes arguments from command line. Run with -r.

Replaces the characters when it finds them;
prints copy if all characters have been replaced; original else.

\$\endgroup\$
1
\$\begingroup\$

JavaScript, 57 bytes

I/O as character arrays.

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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++];
}
\$\endgroup\$
1
\$\begingroup\$

Ruby, 70 64 59 58 bytes

Anonymous function. Walk through the string a to build a new string with letters replaced in accordance to the next character in b and c, then if all characters in b are exhausted at the end, return the newly constructed string, otherwise return the original string.

@histocrat helped save 6 bytes via gsub.

Saved 1 byte thanks to @Cyoce.

->a,b,c{i=0;s=a.gsub(/./){$&==b[i]?c[~-i+=1]:$&};b[i]?a:s}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ You can save a byte by replacing -1+i+=1 with ~-i+=1 \$\endgroup\$
    – Cyoce
    Commented May 4, 2017 at 18:06
0
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.