Substitute a string with itself

Your goal is to substitute a string with itself by replacing each character in the original string with the one before it, starting with the first character and wrapping around. Here are some examples to show what I mean:

1st example:

Input: program
Output: apgopra

Program -> mrogram (replace p by m in program)
-> mpogpam (replace r by p in mrogram)
-> mprgpam (replace o by r in mpogpam)
-> mpropam (replace g by o in mprgpam)
-> mpgopam (replace r by g in mpropam)
-> mpgoprm (replace a by r in mpgopam)
-> apgopra (replace m by a in mpgoprm)

2nd example:

Input: robot
Output: orbro

Robot -> tobot (replace r by t in robot)
-> trbrt (replace o by r in tobot)
-> trort (replace b by o in trbrt)
-> trbrt (replace o by b in trort)
-> orbro (replace t by o in trbrt)

3rd example:

Input: x
Output: x

x -> x (replace x by x in x)

4th example:

Input: xy
Output: xx

xy -> yy (replace x by y in xy)
-> xx (replace y by x in yy)


  • The string x will only contain lowercase alphanumeric characters and spaces
  • This is so shortest code in bytes wins!
  • \$\begingroup\$ Do my edits match your original idea? \$\endgroup\$ – LegionMammal978 Oct 25 '15 at 12:37
  • \$\begingroup\$ Seems fine, I hope people understand that every round they basically encrypt an encrypted string by replacing characters every round. The examples make this clear, so I think they will. \$\endgroup\$ – Thomas W Oct 25 '15 at 12:40

11 Answers 11


CJam, 11 bytes


Test it here.


q__    e# Read input and make two copies.
1m>    e# Rotate the second copy one character to the right.
.{er}  e# For each pair of characters from the second and third string,
       e# replace occurrences of the first with the second.
|improve this answer|||||

TeaScript, 17 bytes 19 21 24

TeaScript is JavaScript for golfing


Nice and short

Try it online (watch for trailing whitespace in the input)

Ungolfed & Explanation

x.reduce     // Reduce over input
  (#         // Anonymous function expands to ((l,i,a)=>
    l.g(        // global replace...
     i          // replace var i with...
     x.cycle(1) // Cycle x 1
     [a]        // At position a
|improve this answer|||||

JavaScript ES6, 69 bytes



#input, #output {
  width: 100%;
<textarea id='input' rows="5">
<div id="output"></div>

|improve this answer|||||
  • 3
    \$\begingroup\$ You can skip the F= in your byte count. \$\endgroup\$ – Ypnypn Oct 25 '15 at 15:47
  • \$\begingroup\$ @Ypnypn Thanks never know what to do when they don't specify this stuff \$\endgroup\$ – George Reith Oct 25 '15 at 23:34
  • \$\begingroup\$ s.slice(i-1)[0] is not equal to s.slice(i-1,i) ? \$\endgroup\$ – edc65 Oct 26 '15 at 11:25
  • 1
    \$\begingroup\$ @edc65 No not when i=0 \$\endgroup\$ – George Reith Oct 26 '15 at 11:28

Ruby, 50 48 bytes



=> "apgopra"
|improve this answer|||||

Mathematica, 89 75 74 57 bytes

|improve this answer|||||
  • \$\begingroup\$ ""<>Fold[#/.#2&,c=Characters@#,Thread[c->RotateRight@c]]& \$\endgroup\$ – alephalpha Oct 25 '15 at 16:20
  • \$\begingroup\$ @alephalpha Thanks, I tried that with Transpose and failed. \$\endgroup\$ – LegionMammal978 Oct 25 '15 at 16:24

Pyth, 13 bytes


Try it online. Test suite.

|improve this answer|||||

k2 - 17 char

Function taking 1 argument.


k2 has a builtin called _ssr for String Search and Replace. _ssr[x;y;z] will find y in x and replace it with z. So we use / to fold this functionality over each replacement we want to make. For those unfamiliar with folding (as in functional programming), essentially _ssr/[x; (y1; y2; y3); (z1; z2; z3)] becomes _ssr[_ssr[_ssr[x; y1; z1]; y2; z2]; y3; z3]. Strings are lists of their characters, so we may simply rotate the input back a step and get the replacements, and plug right in.

  {_ssr/[x;x;-1!x]} "program"
  {_ssr/[x;x;-1!x]} "robot"
  {_ssr/[x;x;-1!x]} (,"x")   / one-letter strings are ,"x" and parens are required
  {_ssr/[x;x;-1!x]} "xy"
|improve this answer|||||

Haskell, 76 bytes

[]#_=[];(x:y)#g@(a,b)|x==a=b:y#g|2>1=x:y#g;h x=foldl(#)x$zip x$last x:init x

Too bad, Haskell doesn't even have a build-in substitution function.

|improve this answer|||||

PHP, 76 bytes

function($s){$f=str_split;echo str_replace($f($s),$f(substr($s,-1).$s),$s);}

Here is the ungolfed version:

function selfSubstitute($originalString)
    $shiftedString = substr($originalString, -1) . $originalString;

    $splitOriginalString = str_split($originalString);
    $splitShiftedString = str_split($shiftedString);

    echo str_replace($splitOriginalString, $splitShiftedString, $originalString);
|improve this answer|||||

Python, 67 64 62 57 Bytes

Straightforward solution, will look into something to shorten this. Thanks to @RandyC for saving 5 bytes.

for x in zip(c,c[-1]+c):c=c.replace(*x)
print c

Input should be in quotes.

|improve this answer|||||
  • \$\begingroup\$ You can save a few bytes dropping the [:-1] since zip truncates to the shortest iterable. \$\endgroup\$ – Randy Oct 26 '15 at 21:24
  • \$\begingroup\$ @RandyC Wow, good call! Thanks. \$\endgroup\$ – Kade Oct 27 '15 at 2:18

Haskell, 58 bytes

r(x,y)c|x==c=y|0<1=c;f s=foldl(flip$map.r)s.zip s$last s:s

Pretty similar to Christian's solution, but using map and the fact that zip ignores superfluous elements if the lists are of unequal length. It folds through the list of replacements (on the form (from,to)), updating the string by mapping the hand written replacement function r on each letter.

The expression flip$map.r was derived using LambdaBot's "Pointless" plugin.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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