Problem 604. Next lexicographic - permutation
Find next lexicographic - permutation (permutations as it would occur in a dictionary order).
E.g: nextP('ABCD') = ABDC
If you can generate the next permutation, then you can also generate a 'cycle' of all permutations using a construct like,
cycle = +1;
curr = start;
while ( true )
curr = nextP(curr);
if ( curr == start )
break;
end
cycle = cycle+1;
end
For fun, you could generate all the n! permutations of a, unique n-letter string.
Solution Stats
Problem Comments
-
2 Comments
Stefan Abendroth
on 20 Jul 2023
I think test case 6 is wrong. The lexicographic follower of 'xyzDOG' is 'xyzGDO', not 'xyzGOD'.
Dyuman Joshi
on 21 Jul 2023
@Stefan, if you see carefully, test case 6 asks for the *next to next* lexicographic permutation of 'DOG', which is 'GOD'.
Solution Comments
Show commentsGroup

Combinatorics I
- 15 Problems
- 28 Finishers
- Repopulate the City
- Next lexicographic - permutation
- Create Combination
- Combinations without using nchoosek
- NCHOOSEK - Time Optimization
- Usage of java.math : N Choose K with unlimited precision
- Get ranking of a combination
- Get ranking of a combination looking for speed.
- Put m balls into n boxes
- Put m balls into n boxes (again)
- Numbered lottery balls into cells
- A (wrong) place for everything, and everything in its (wrong) place
- Possible Outcomes of American Roulette
- Which permutation is it?
- N-th permutation.
Problem Recent Solvers49
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!