allAnagrams
const allAnagrams = (string) => {
var arr = string.split('');
// let arr = Array.from(string);
let results = [];
for (let permutation of permute(arr)) {
results.push(permutation.join(''));
}
return results;
};
const permute = function* (permutation) {
const length = permutation.length;
let c = Array(length).fill(0);
let i = 1;
yield permutation;
// iterate through
while (i < length) {
if (c[i] < i) {
var k = (i % 2) ? c[i] : 0;
var p = permutation[i];
permutation[i] = permutation[k];
permutation[k] = p;
c[i]++;
i = 1;
yield permutation;
} else {
c[i] = 0;
i++;
}
}
};
const test = allAnagrams('test');
console.log(test);
This Gist was automatically created by Carbide, a free online programming environment.