• + 0 comments

    My general algorithm for solving this is as follows: Let's use "dkhc" as the driving example.

    1. Start looking from the right, and find the first pair of letters that is out of order. Here, 'd' and 'k' are out of order.
    2. Find the smallest letter to the right of the out-of-order letter that is greater than it. Here, 'h' is the smallest letter that is greater than 'd'.
    3. Swap those two letters. This gives us "hkdc".
    4. Finally, sort all the letters to the right of the original out-of-order index. Here, that means sorting "kdc" into "cdk". Altogether then, "hcdk" is the final answer.