Find the N | 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
function string_permutation ( n , str ) { let s = [ ] ; let result = ' ' ; n = n - 1 ; for ( let i = 1 ; i < str . length + 1 ; i ++ ) { s . push ( n % i ) ; n = Math . floor ( n / i ) ; } let len = str . length ; for ( let i = 0 ; i < len ; i ++ ) { let a = s [ s . length - 1 ] ; result += str [ a ] ; let j ; for ( j = a ; j < str . length ; j ++ ) str = str . substring ( 0 , j ) + str . charAt ( j + 1 ) + str . substring ( j + 1 ) ; str = str . substring ( 0 , j + 1 ) ; s . pop ( ) ; } document . write ( result , ' ' ) ; } let str = " " ; n = 11 ; string_permutation ( n , str ) ;