Solve string permutations problem in ruby