Sep 28, 2017

Next Permutation is the immediate lexicographically larger permutation of a sequence.

In most languages there is a function already implemented.

In C++
string s("abcd"); 
next_permutation(s.begin(),s.end());
cout<<s;
will give
abdc
Note that when a string is already the lexicographic largest, next_permutation returns 0 and hence doesn't loop forever if given in a while loop.