patternjavascriptMinor
Recursive function that generates the permutations of a string
Viewed 0 times
generatesthefunctionrecursivethatstringpermutations
Problem
I am looking for a review of my recursive function that generates the permutations of a string. Are there better ways to do this?
var permutations = [];
function doPerm(str, arr) {
if (typeof (str) == 'string') str = str.split('');
if (str.length == 0) permutations.push(arr.join(''));
for (var i = 0; i < str.length; i++) {
var x = str.splice(i, 1);
arr.push(x);
doPerm(str, arr);
arr.pop();
str.splice(i, 0, x);
}
}
doPerm('', []);
console.log(permutations);Solution
I modified your code after getting the error message: "too much recursion" for string of about 6 characters or so."
Some of the names are long, but descriptive.
Features:
Anything else you can add of value or take away would be appreciated.
//====================================================
function getPermutations(str){
//Enclosed data to be used by the internal recursive function permutate():
var permutations = [], //generated permutations stored here
nextWord = [], //next word builds up in here
chars = [] //collection for each recursion level
;
//---------------------
//split words or numbers into an array of characters
if (typeof str === 'string') chars = str.split('');
else if (typeof str === 'number') {
str = str + ""; //convert number to string
chars = str.split('');//convert string into char array
}
//============TWO Declaratives========
permutate(chars);
return permutations;
//===========UNDER THE HOOD===========
function permutate(chars){ //recursive: generates the permutations
if(chars.length === 0)permutations.push(nextWord.join(''));
for (var i=0; i < chars.length; i++){
chars.push(chars.shift()); //rotate the characters
nextWord.push(chars[0]); //use the first char in the array
permutate(chars.slice(1)); //Recurse: array-less-one-char
nextWord.pop(); //clear for nextWord (multiple pops)
}
}
//--------------------------------
}//==============END of getPermutations(str)=============Some of the names are long, but descriptive.
Features:
- No longer get the "too much recursion" message.
- Only one argument is passed to the function.
- Takes a string or a number argument.
- Variables are not global but enclosed in the outer function.
- The inner function does the recursion using closure variables
- Doesn't use splice, but uses slice only once.
Anything else you can add of value or take away would be appreciated.
Code Snippets
//====================================================
function getPermutations(str){
//Enclosed data to be used by the internal recursive function permutate():
var permutations = [], //generated permutations stored here
nextWord = [], //next word builds up in here
chars = [] //collection for each recursion level
;
//---------------------
//split words or numbers into an array of characters
if (typeof str === 'string') chars = str.split('');
else if (typeof str === 'number') {
str = str + ""; //convert number to string
chars = str.split('');//convert string into char array
}
//============TWO Declaratives========
permutate(chars);
return permutations;
//===========UNDER THE HOOD===========
function permutate(chars){ //recursive: generates the permutations
if(chars.length === 0)permutations.push(nextWord.join(''));
for (var i=0; i < chars.length; i++){
chars.push(chars.shift()); //rotate the characters
nextWord.push(chars[0]); //use the first char in the array
permutate(chars.slice(1)); //Recurse: array-less-one-char
nextWord.pop(); //clear for nextWord (multiple pops)
}
}
//--------------------------------
}//==============END of getPermutations(str)=============Context
StackExchange Code Review Q#59615, answer score: 5
Revisions (0)
No revisions yet.