Guest User

Untitled

a guest
Jun 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. function stringPermutations(string) {
  2. var results = [];
  3.  
  4. if (string.length === 1) {
  5. results.push(string);
  6. return results;
  7. }
  8.  
  9. for (var i = 0; i < string.length; i++) {
  10. var firstChar = string[i];
  11. var charsLeft = string.substring(0, i) + string.substring(i + 1);
  12. var innerPermutations = stringPermutations(charsLeft);
  13. for (var j = 0; j < innerPermutations.length; j++) {
  14. results.push(firstChar + innerPermutations[j]);
  15. }
  16. }
  17.  
  18. return results;
  19. }
Add Comment
Please, Sign In to add comment