F5 interview question

Some variation of Permutations II from LeetCode