To find all possible permutations of a given string

This is just to solve a problem that looks pretty interesting. I tried to think about it, but could not find a way to solve this problem in an effective time frame. Perhaps my concepts are still growing ... anyway, the question is the following.

It is required to find out all the possible permutation of this line ....... Also, share if there may be any possible options for this problem.

I found a solution on the network that uses recursion .. but this is not satisfactory since it looks a bit erroneous.

The program is as follows: -

void permute(char s[], int d)
{
   int i;

   if(d == strlen(s))
      printf("%s",s);

   else
   {
      for(i=d;i<strlen(s);i++)
      {
         swap(s[d],s[i]);
         permute(s,d+1);
         swap(s[d],s[i]);
      }
   }
}

If this program looks good (it gives an error when I ran it), please give a small example to understand this, as I am still developing recursion concepts.

, .

, , HW........

.............

+3
2

, ​​, . : , main swap ( swap , swap(s, d, i)).

, , printf("permute(%s, %d)", s, d) permute, 3 4- .

, permute d; , d, , , (.. ). permute d. , (d= 0) permute 0, (d= 1) 1, , 0 .. (d= n -1) , (d= n) .

Θ (n · n!), , . , , strlen(s) Θ (n² · n!); Θ (n · n!). Θ (n) , , .

+4

. .

. -, swap C, C . , , .

strlen . , . (- swap s), , . . '\0' , .

+1

Source: https://habr.com/ru/post/1757536/


All Articles