Thank you for your solution! It works perfectly. I think it solves the problem like "abb", swap a and first b we can get "bab. But if we swap a and the second b then swap a and the middle b, we also can get "bab". So the duplicates are removed.