Absolute Permutation, Algorithms
prabaprakash opened this issue · 3 comments
prabaprakash commented
Please list possible algo for solving these problems
prabaprakash commented
let permArr = [],
usedChars = [];
function permute(input) {
// console.log(input)
var i, ch;
for (i = 0; i < input.length; i++) {
// fixed 1, then process [2,3]
ch = input.splice(i, 1)[0];
// usedchars [1]
usedChars.push(ch);
if (input.length == 0) {
permArr.push(usedChars.slice());
}
// processing [2,3]
permute(input);
//
input.splice(i, 0, ch);
usedChars.pop();
}
return permArr
};
process.stdout.write(JSON.stringify(permute([1, 2, 3])));prabaprakash commented
Other methods recursive
function perm(xs) {
let ret = [];
for (let i = 0; i < xs.length; i++) {
let currentChar = xs.splice(i, 1)[0];
let rest = perm(xs);
if (!rest.length) {
ret.push([currentChar])
} else {
for (let j = 0; j < rest.length; j++) {
ret.push([currentChar].concat(rest[j]))
}
}
xs.splice(i, 0, currentChar);
}
return ret;
}prabaprakash commented
Understanding question plays a major rule, these one is nothing to do with permutation.
