updated string permutation with dynamic programming