Solution for permutation problem