String permutations algorithm.