This is my first code golf challenge, as such I am not entirely certain if it is suitable as a Code Golf challenge, but it's all a learning experience.

The challenge is that in as few characters as possible, using whichever language you prefer, develop a code to complete the following:

  1. Take a value from stdin
  2. Return a value to stdout that contains the same characters as the value at stdin in a different order. Not necessarily random, but following the restrictions set out below.

Here are a few restrictions:

  • All characters that are contained in the stdin value must appear in stdout value
  • Characters must appear in stdout the same amount of times they appear in stdin
  • The order of characters in the output, cannot be identical to the order of characters in input, for more than two consecutive characters. Regardless of the source of the character in the input. That is, the input and output must not have a common substring of length 3 or greater.
  • Any input where no solution can be found under the previous rule, is considered an invalid input

    Examples: aaaa, bbbb

  • you do not need to test for invalid inputs, assume all inputs are valid.

  • The length of stdout must be the same as the length of stdin
  • You may use any method to scramble the initial value, there is nothing off limits here.


stdin: Hello There

stout: loTeHle reh

Note: a character in this context can be any valid ASCII character


3 Answers 3


CJam, 31 27 bytes


Test it here.

Same approach as my Mathematica answer. It keeps generating random permutations of the input, until the substring restriction is satisfied. I think this can be golfed a bit further.


Mathematica, 89 bytes


Generates random permutations until there is the permutation and the original have no common substring of length 3.

  • \$\begingroup\$ I don't think @"HelloThere" needs to be counted towards the size. \$\endgroup\$
    – kennytm
    Commented Jan 27, 2015 at 13:58
  • \$\begingroup\$ @KennyTM oh, thank you, totally forgot to remove that \$\endgroup\$ Commented Jan 27, 2015 at 14:48

CJam, 48 bytes

This is super huge. I am not sure if there are any good algorithms to do the task. This is just a brute force.


Try it online here

  • \$\begingroup\$ for aacc, the program returned acca, which unforetunately fails the third restriction. \$\endgroup\$
    – Sam Weston
    Commented Jan 27, 2015 at 21:52

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.