We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
My general algorithm for solving this is as follows:
Let's use "dkhc" as the driving example.
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.
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'.
Swap those two letters. This gives us "hkdc".
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.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Bigger is Greater
You are viewing a single comment's thread. Return to all comments →
My general algorithm for solving this is as follows: Let's use "dkhc" as the driving example.