Here is the interview question prompt, presented for reference.
Write an algorithm that takes an input string like "abc"
, and prints out all possible permutations of the string. A permutation
of a group of elements is defined as one of the n!
number possible arrangements the elements can take, where n
is the number of elements in the range.
We'd expect the following to hold true, note the permutations printed out:
const str = "abc"
permutations(str)
// abc
// acb
// bac
// bca
// cba
// cab
String str = "abc";
permutations(str);
// abc
// acb
// bac
// bca
// cba
// cab
str = "abc"
permutations(str)
# abc
# acb
# bac
# bca
# cba
# cab
std::string str = "abc";
permutations(str);
// abc
// acb
// bac
// bca
// cba
// cab
string str = "abc";
Permutations(str);
// abc
// acb
// bac
// bca
// cba
// cab
str := "abc"
permutations(str)
// abc
// acb
// bac
// bca
// cba
// cab
A hint here: break the problem down, and think about what steps are going to be repeated. How would we get from "abc"
to "acb"
?
7
O(n*n!)
O(n*n!)
You can see the full challenge with visuals at this link.
Challenges • Asked almost 7 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Generate All String Permutations.