Write a C program to print all permutations of a given string | GeeksforGeeks
A permutation, also called an “arrangement number” or “order,” is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.
O(n*n!)
}
http://ideone.com/D0WUhF
A permutation, also called an “arrangement number” or “order,” is a rearrangement of the elements of an ordered list S into a one-to-one correspondence with S itself. A string of length n has n! permutation.
O(n*n!)
void
permute(
char
*a,
int
i,
int
n)
{
int
j;
if
(i == n)
printf
(
"%s\n"
, a);
else
{
for
(j = i; j <= n; j++)
{
swap((a+i), (a+j));
permute(a, i+1, n);
swap((a+i), (a+j));
//backtrack
}
}
http://ideone.com/D0WUhF
- static void permute(int i) {
- int j;
- if (i == s.length - 1)
- else {
- for (j = i; j < s.length; j++) {
- swap(i, j);
- permute(i + 1);
- swap(i, j); // backtrack
- }
- }
- }