For higher input it not works it gives fatal error : CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of memory what i do for running the same algorithm (that means using the recursion) and it successfully runs not gives the heap out of memory
/*CREATE THE ALL POSSIBLE PERMUTATION OF STRING.*/
var counter = 0;
function permutation(str, start, end) {
if (start === end) {
counter++;
console.log(str + "---" + counter);
}
else
for (let i = start; i < end; i++) {
str = swap(str, start, i);
permutation(str, start + 1, end);
}
}
/**
* swaps the two chars of a particular string
*/
function swap(str, swapTo, swapWith) {
let swapToChar = str[swapTo];
let swapWithChar = str[swapWith];
let result = "";
for (i = 0; i < str.length; i++) {
let newLetterToAppend = str[i];
if (i == swapTo)
newLetterToAppend = swapWithChar;
else if (i == swapWith)
newLetterToAppend = swapToChar;
result = result + "" + newLetterToAppend;
}
return result;
}
let str = "%4546 dfvbgjb ";
permutation(str, 0, str.length);