Haven't taken the time to read through this properly so forgive the question - but is this any different from the above problem?
And if so - if someone solves this programming task on leetcode have they not then also solved the permutation problem from OP?