Find the N | C # program to find the N - th lexicographic permutation of String using Factroid method ; Function to calculate nth permutation of String ; Creating an empty stack ; Subtracting 1 from N because the permutations start from 0 in factroid method ; Loop to generate the factroid of the sequence ; Loop to generate nth permutation ; Remove 1 - element in each cycle ; Final answer ; Driver code
using System ; using System . Collections . Generic ; class GFG { static void String_permutation ( int n , String str ) { Stack < int > s = new Stack < int > ( ) ; String result = " " ; n = n - 1 ; for ( int i = 1 ; i < str . Length + 1 ; i ++ ) { s . Push ( n % i ) ; n = n / i ; } for ( int i = 0 ; i < str . Length ; i ++ ) { int a = s . Peek ( ) ; result += str [ a ] ; int j ; for ( j = a ; j < str . Length - 1 ; j ++ ) { str = str . Substring ( 0 , j ) + str [ j + 1 ] + str . Substring ( j + 1 ) ; } str = str . Substring ( 0 , j + 1 ) ; s . Pop ( ) ; } Console . Write ( result + " STRNEWLINE " ) ; } public static void Main ( String [ ] args ) { String str = " abcde " ; int n = 11 ; String_permutation ( n , str ) ; } }