Here is a quick algorithm that creates a short string containing all permutations. I am sure that he gives the shortest answer, but I do not have complete evidence in my hand.
Explanation. Below is a tree of all permutations. The image is incomplete; imagine that the tree goes on forever to the right.
1 --+-- 12 --+-- 123 ... | | | +-- 231 ... | | | +-- 312 ... | +-- 21 --+-- 213 ... | +-- 132 ... | +-- 321 ...
The nodes at level k of this tree are all permutations of length k. In addition, permutations in a certain order with a large amount of overlap between each permutation and its neighbors above and below.
To be precise, each node of the first child is found by simply adding the next character to the end. For example, the first child of 213 will be 2134. The rest of the children can be found by turning to the first child to leave one character at a time. A rotation of 2134 will produce 1342, 3421, 4213.
Taking all the nodes at a given level and linking them together, overlapping as much as possible, creates lines 1, 121, 123121321, etc.
The length of the string n in this sequence is the sum for x = 1 to n of x! . (You can prove this by observing how much does not overlap between adjacent permutations. Siblings overlap in all characters except one, the first cousins overlap in all but two characters, etc.)
Sketch of evidence. I have not fully proved that this is the best solution, but here is a sketch of how the proof will continue. First show that any string containing n different permutations is & ge; 2 n - 1. Then show that adding any line containing n + 1 different permutations is 2 n + 1. That is, adding another permutation will cost you two digits. Continue to calculate the minimum length of lines containing n P r and n P r + 1 different permutations, up to n !. In short, this sequence is optimal because you cannot make it worse somewhere in the hope of making it better somewhere else. It is already locally optimal everywhere. All movements are forced.
Algorithm. Given all this background, the algorithm is very simple. Walk this tree to the desired depth and connect all the nodes at this depth.
Fortunately, we actually do not need to build a tree in memory.
def build(node, s): """String together all descendants of the given node at the target depth.""" d = len(node) # depth of this node. depth of "213" is 3. n = len(s) # target depth if d == n - 1: return node + s[n - 1] + node # children of 213 join to make "2134213" else: c0 = node + s[d] # first child node children = [c0[i:] + c0[:i] for i in range(d + 1)] # all child nodes strings = [build(c, s) for c in children] # recurse to the desired depth for j in range(1, d + 1): strings[j] = strings[j][d:] # cut off overlap with previous sibling return ''.join(strings) # join what left def stringContainingAllPermutationsOf(s): return build(s[:1], s)
Performance. The above code is already much faster than my other solution, and it cuts and inserts large lines a lot, which you can optimize. The algorithm can be executed in time and in memory proportional to the size of the output.