string permutation algorithm