SHELPASS - Editorial

Sheldon Password

Problem
practice

Tags: Permutation.

Author: Shami.

This problem is a fancy way of asking to compute Kth permutation given a starting string. Since the maximum length of string is only 9, we can map the letters to digits and work with numbers (if that is more comfortable).

How to find the next permutation?

This Wiki article describes the following steps:

  1. Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation.
  2. Find the largest index l greater than k such that a[k] < a[l].
  3. Swap the value of a[k] with that of a[l].
  4. Reverse the sequence from a[k + 1] up to and including the final element a[n].

However, if you are programming in C++, you’ll be happy to know there is a function that just does that!