← ALL NOTES
Hash Table

3720. Lexicographically Smallest Permutation Greater Than Target

The winning move is to diverge from target as late as possible: keep a permutation of s equal to target for the longest prefix you can, then place the smallest letter that beats it and sort the rest.

Loading…