String permutation - working, but can be optimized more