Perl, 51 bytes
$s=<>;$s=~s=^.*$_=$_=,$,.=$&for split"",<>;print$,;
Input is provided via STDIN. First input is the starting word (e.g. chameleon
), second input is the letters as a single string (e.g. caln
).
The above is just aan obfuscated (read "prettier") way of doing the following:
$word = <>;
for $letter(split "", <>) {
$word =~ s/^.*$letter/$letter/;
$result .= $&;
}
print $result;
As we go through each letter, we replace from the start of the word up to the letter in the source word with just the new letter, and append the match (stored in $&
) to our result. Since the match includes the letter and then gets replaced with the letter, each letter ends up appearing twice.
Because STDIN appends a new line character to both of our inputs, we're guaranteed to capture the remnants of the full word on the last match, i.e. the new line character.