Find the N | Java 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
import java . util . * ; class GFG { static void String_permutation ( int n , String str ) { Stack < Integer > s = new Stack < Integer > ( ) ; String result = " " ; n = n - 1 ; for ( int i = 1 ; i < str . length ( ) + 1 ; i ++ ) { s . add ( n % i ) ; n = n / i ; } for ( int i = 0 ; i < str . length ( ) ; i ++ ) { int a = s . peek ( ) ; result += str . charAt ( a ) ; int j ; for ( j = a ; j < str . length ( ) - 1 ; j ++ ) str = str . substring ( 0 , j ) + str . charAt ( j + 1 ) + str . substring ( j + 1 ) ; str = str . substring ( 0 , j + 1 ) ; s . pop ( ) ; } System . out . print ( result + "NEW_LINE"); } public static void main ( String [ ] args ) { String str = " abcde " ; int n = 11 ; String_permutation ( n , str ) ; } }