Basically we want to increase the first character we can from the right by the smallest possible amount and make the smallest possible permutation (we'll get to how we do this) with the characters remaining to the right of it. Why this is what we want to do is, I hope, pretty logical. If it isn't, it may help to think in terms of a permutation of digits (making a number) - you could keep adding one to the number until you get to another permutation of that number, though a way more efficient method would be to magically find the left-most number we need to increase and the amount to increase it by and simply get the smallest permutation of the digits to the right, which is exactly what this algorithm does.
Well, we can't just increase a character to any other character, it needs to be a character that already exists in the permutation. And if this character exists to the left, that won't really help, as it will require changing the character that's to the left as well, which will have us potentially end up with a much larger permutation. So we need to use a character to the right and, on top of this, it must be the smallest character that is bigger than this character.
So, go from the right and, for each character A, look right for the smallest character larger than A (call this B). If you don't find one, try the next character. The above paragraph explains that we want to increase A to B's value. Once we've done this, we are left with 2 B's, now, to fix this, we decrease B to A's value (this is essentially just swapping A and B). From here we fix everything to the right of where A was. Since we're going to do this fixing, it doesn't really matter that the new A now might not be where it should be).
How do we fix the remaining characters? Well, we want the smallest permutation of them, which is simply those characters ordered.
That takes care of steps 2-4.
What about step 1? This is actually just an optimization of the algorithm. If we're going from the right and finding the first character for which there exists a larger character to the right, won't it make sense that we need to find the first character that's smaller than a character to the right of it, or, more specifically, the character one position to the right of it? Think about 987654321
- we can't do anything here since all the characters are larger than the characters directly to their right.
To explain with an example:
Take ABDC
.
The first character for which there exists a larger character to the right is B
. The smallest character larger than B
is C
.
So the next possible permutation will look like: AC??
(?
is unknown)
The two remaining characters are D
and B
, which we need to find the smallest permutation of, which is BD
. So we put it together: ACBD
.
In terms swapping B
and C
- we need B
to become C
(from paragraph 2), but there still needs to be a B
in the string for it to be a valid permutation (so we can't just replace B
with C
) and it doesn't matter where B
ends up, since, right afterwards, we find the smallest permutation of the remaining characters.