Solved NextPermutation problem