how to find nth permutation of a string in c.write a c program to find nth permutation of a string. For example if given string is “1256” then all permutation of string “1256” would be

So if nth is 8 then nth permutation of string “1256” will be 2 1 6 5 ( i.e 7th for 0 base index ).

Let us write c program to find all permutation of given string and later on we would modify this program to find nth permutation of string.

The above program will print all permutation of “1256”.

how to find nth permutation of a string in c

From above output followings are the observation

  • The length of given string “125”  is 3 and its total permutation count is 6 i.e factorial 3.
  • In this way we can modify the base case of above program to find nth permutation. See below code.

Output : 2165

optimization opportunity

The above program to find nth permutation is computing all permutation of a given string. can you think what are some optimizations require for faster execution or about reducing complexity.




Related Contents to follow