First of all we check maximum number of permutations possible from the given string … if it is < n than answer is impossible.
Now we have to fix character at each position starting from MSB position
For fixing the character at any position initially we try to fix smallest character at that position if by placing that character number of permutation formed >= remaining required permutation than fix this charter at that position , and decrease the frequency of this character since this character is used at this position and move to next position , else if no of permutation formed by placing this character is less than the required remaining permutation than decrease remaining permutation by the number of permutation formed by placing this character at that position ( since this count times number of permutations comes in between current to final permutation ) and try to find next character for this position .