### The Problem

Here’s a problem I was asked recently:

Write a function permute such that:

`permute('abc') → ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']`

Now, immediately this should look like a recursive problem. Put in English, we want to do the following:

- Iterate through the initial string – e.g., ‘abc’.
- For each character in the initial string, set aside that character and get a list of all permutations of the string that’s left. So, for example, if the current iteration is on
`'b'`

, we’d want to find all the permutations of the string`'ac'`

. - Once you have the list from step 2, add each element from that list to the character from the initial string, and append the result to our list of final results. So if we’re on
`'b'`

and we’ve gotten the list`['ac', 'ca']`

, we’d add`'b'`

to those, resulting in`'bac'`

and`'bca'`

, each of which we’d add to our final results. - Return the list of final results.

I was asked this in a somewhat high-pressure situation, and although I could come up with this explanation in English, I couldn’t produce the solution as cleanly as I would have liked. I’m writing it up here both to cement my own understanding of the problem and to help people get clear on what really is a fairly basic example of recursion in Python.

### A Solution … Sorta

I eventually came up with something like this:

def permute1(start, rest): res = [] if len(rest) <= 1: res += [start + rest, rest + start] else: for i, c in enumerate(rest): s = rest[:i] + rest[i+1:] for perm in permute1(c, s): res += [start + perm] return res

This code works, after a fashion:

>>> permute('', 'abc') ['abc', 'acb', 'acb', 'abc', 'bac', 'bca', 'bca', 'bac', 'cab', 'cba', 'cba', 'cab']

But it’s embarrassing. For one thing, it produces duplicates in our list, which is obviously not what we want. For another, it takes two arguments, not one. In other words, it doesn’t solve the problem.

My initial inclination to solve the first problem was to use Python’s built-in set datatype. This was overkill, as it turns out. The duplicates are being caused by the line `res += [start + rest, rest + start]`

. Imagine if `start`

is `'b'`

and `rest`

is `'c'`

. Then the algorithm will, in the base case, add `['bc', 'cb']`

to `res`

before passing it back. This is just unnecessary, since our for loop below will catch the `'cb'`

case later on. So a quick fix to that line, adding just `[start + rest]`

, will fix the first issue.

However, a correct solution would only take one argument, so we’re still not quite there.

### Don’t Overthink Recursion

The problem really is that I was overthinking it. Here’s a trick when it comes to solving recursive problems: just write your function as though when you call it recursively it’ll work as expected. In the English explanation above I had a bit about getting a list of all the permutations of the remaining string. Obviously, that’s exactly the same problem as getting a list of all permutations of the original string! So I just needed to hammer out the problem and not get ahead of myself.

When I sat down to just type, here’s what came out:

def permute2(s): res = [] if len(s) == 1: res = [s] else: for i, c in enumerate(s): for perm in permute2(s[:i] + s[i+1:]): res += return res

When you run this, you get the following:

>>> permute2('abc') ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']

Much nicer! Not only does it work as expected, but the code feels cleaner than before. Also note that, in both examples, I use Python’s built-in enumerate() to avoid the fairly un-Pythonic tricks using `range(len(s))`

you see in places like this.

### Bonus

For the masochists, here’s a condensed version using the ternary operator and list comprehensions:

def permute3(s): return [s] if len(s) == 1 else +s[i+1:])]

Don’t use this.

Great post! I found this page helpful as well:

http://www.programmerinterview.com/index.php/recursion/permutations-of-a-string/

Quiet elegant solution.

Nice little piece of code, but for practical reasons;

permutations = lambda s: (“”.join(i) for i in itertools.permutations(s))

list(permutations(“abc”))

> import itertools

> [ “”.join(x) for x in itertools.permutations(‘abc’)]

[‘abc’, ‘acb’, ‘bac’, ‘bca’, ‘cab’, ‘cba’]

Standard library to the rescue (again!)

> There should be one– and preferably only one –obvious way to do it.

def permute(letters):

return [“”.join(p) for p in itertools.permutations(letters, len(letters))]

Thanks, all. Yeah, of course itertools is the right way to handle this. But it’s not the sort of thing you can suggest in an interview. Or if you do, you say “Actually, I’d just use itertools” and then get down to implementing the algorithm anyway.

You can try this for the first solution to remove the duplicates:

def permute1(start, rest):

res = []

if len(rest) <= 1:

res += [start + rest, rest + start]

else:

for i, c in enumerate(rest):

s = rest[:i] + rest[i+1:]

for perm in permute1(c, s):

res += [start + perm]

return set(res)

Hi.. Your solution looks pretty good!

Can anyone please help to find anagrams of a string with a single recursive call instead of n(length of string) recursive calls?

Thank you for the solutions! Unfortunately, it doesn’t solve for already-duplicate words such as “ssssss”

I think using something like HashSet might be feasible solution, while leaving the permutation logic to the first solution.