Find the N | Function to calculate nth permutation of string ; Creating an empty list ; 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
def string_permutation ( n , str ) : NEW_LINE INDENT s = [ ] NEW_LINE result = " " NEW_LINE str = list ( str ) NEW_LINE n = n - 1 NEW_LINE for i in range ( 1 , len ( str ) + 1 ) : NEW_LINE INDENT s . append ( n % i ) NEW_LINE n = int ( n / i ) NEW_LINE DEDENT for i in range ( len ( str ) ) : NEW_LINE INDENT a = s [ - 1 ] NEW_LINE result += str [ a ] NEW_LINE for j in range ( a , len ( str ) - 1 ) : NEW_LINE INDENT str [ j ] = str [ j + 1 ] NEW_LINE DEDENT str [ j + 1 ] = ' \0' NEW_LINE s . pop ( ) NEW_LINE DEDENT print ( result ) NEW_LINE DEDENT str = " abcde " NEW_LINE n = 11 NEW_LINE string_permutation ( n , str ) NEW_LINE