The function takes a string as input, update the string to the previous one in lexicographical order if there is, and return a bool value. If there is a smaller string, return true; if not, return false and update the string from the smallest to the greatest.
Idea:
- Search back from the end, find the first greater character compared to the one immediately after it.
- Search back from the end again, find the first smaller one compared to the great character just found.
- Swap the 2 characters found from above 2 steps.
- Reverse the rest string after the character location found in Step 1.
No comments:
Post a Comment