I am currently trying to find the various permutations of a string. The code used works for strings with 5 characters. But With more than 5, it throws an error -"too much recursion".
function doPerm(str, arr) {
if (typeof (str) == 'string')
str = str.split('');
if (str.length === 0){
if(allowedPerm(arr.join('')))
permutations.push(arr.join(''));
}
for (var i = 0; i < str.length; i++) {
var x = str.splice(i, 1);
//console.log(i+"--"+str);
arr.push(x);
doPerm(str, arr);
arr.pop();
str.splice(i, 0, x);
}
}
//doPerm("abcde", []);//works returns 120
doPerm("abcdefa", []);
What can I change to avoid this??. And also is there a better way to do it??