This was INTENSE! By far the hardest algorithm so far I think… this one made me feel like an absolute novice.
After much deliberation ended up implementing a non-recursive version of Heap’s algorithm…
let n = str.length
let array = str.split('')
let count = 0;
let checkRepeats = (x) => {
return /(.)+\1/g.test(x.join(''))
}
let c = [];
for(let i = 0; i < n; i++){
c[i] = 0
}
checkRepeats(array) ? null : count++
let i = 0;
while(i < n){
if(c[i] < i){
if(i % 2 == 0){
[array[0], array[i]] = [array[i], array[0]]
} else {
[array[c[i]], array[i]] = [array[i], array[c[i]]]
}
checkRepeats(array) ? null : count++
c[i] += 1
i = 0
} else {
c[i] = 0
i++
}
}
return count
}