But it doesn't have to end here! Sign up for the 7-day coding interview crash course and you'll get a **free** Interview Cake problem every week.

**
You're in!
**

**Write a recursive function for generating all permutations of an input string. Return them as a set.**

Don't worry about time or space complexity—if we wanted efficiency we'd write an iterative version.

To start, assume every character in the input string is unique.

Your function can have loops—it just needs to also be recursive.

Make sure you have a base case! Otherwise your function may never terminate!

Let's break the problem into subproblems. How could we re-phrase the problem of getting all permutations for "cats" in terms of a smaller but similar subproblem?

**Let's make our subproblem be getting all permutations for all characters except the last one**. If we had all permutations for "cat," how could we use that to generate all permutations for "cats"?

We could put the "s" in each possible position for each possible permutation of "cat"!

These are our permutations of "cat":

cat
cta
atc
act
tac
tca

For each of them, we add "s" in each possible position. So for "cat":

cat
scat
csat
cast
cats

And for "cta":

cta
scta
csta
ctsa
ctas

And so on.

Now that we can break the problem into subproblems, we just need a base case and we have a recursive algorithm!

If we're making all permutations for "cat," we take all permutations for "ca" and then put "t" in each possible position in each of those permutations. We use this approach recursively:

unordered_set<string> getPermutations(const string& inputString)
{
unordered_set<string> permutations;
// base case
if (inputString.length() <= 1) {
permutations.insert(inputString);
return permutations;
}
string allCharsExceptLast = inputString.substr(0, inputString.length() - 1);
char lastChar = inputString[inputString.length() - 1];
// recursive call: get all possible permutations for all chars except last
const auto permutationsOfAllCharsExceptLast = getPermutations(allCharsExceptLast);
// put the last char in all possible positions for each of the above permutations
for (const string& permutationOfAllCharsExceptLast : permutationsOfAllCharsExceptLast) {
for (size_t position = 0; position <= allCharsExceptLast.length(); ++position) {
string permutation = permutationOfAllCharsExceptLast.substr(0, position)
+ lastChar + permutationOfAllCharsExceptLast.substr(position);
permutations.insert(permutation);
}
}
return permutations;
}

How does the problem change if the string can have duplicate characters?

What if we wanted to bring down the time and/or space costs?

This is one where *playing with a sample input* is huge. Sometimes it helps to think of algorithm design as a two-part process: *first* figure out how you would solve the problem "by hand," as though the input was a stack of paper on a desk in front of you. *Then* translate that process into code.

Reset editor

Powered by qualified.io

{"id":22270485,"username":"2021-09-28_14:12:09_+wu&53","email":null,"date_joined":"2021-09-28T14:12:09.764516+00:00","first_name":"","last_name":"","full_name":"","short_name":"friend","is_anonymous":true,"is_on_last_question":false,"percent_done":0,"num_questions_done":0,"num_questions_remaining":46,"is_full_access":false,"is_student":false,"first_payment_date":null,"last_payment_date":null,"num_free_questions_left":3,"terms_has_agreed_to_latest":false,"preferred_content_language":"","preferred_editor_language":"","is_staff":false,"auth_providers_human_readable_list":"","num_auth_providers":0,"auth_email":""}

. . .