Javascript String permutations

Description

Javascript String permutations

//Write a recursive function for generating all permutations of 
//an input string. Return them as an array.

function permuteString(str) {
  var results = [];
  var newStr = "";
  (function recurse() {
    if (newStr.length >= str.length) {
      results.push(newStr);//  w w w.  j a va 2s  . co m
      return;
    }

    for (var i = 0;i < str.length;i++) {
      newStr += str[i];
      recurse();
      newStr = newStr.substring(0, newStr.length - 1);
    }
  })();
  return results;
}

var str = "as";
console.log(permuteString(str));



PreviousNext

Related