Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. var myString = "xyz";
  2.  
  3. function printPermut(inputString){
  4. var outputString;
  5. if(inputString.length === 0){
  6. return inputString;
  7. }
  8. if(inputString.length === 1){
  9. return inputString;
  10. }
  11. else{
  12. for(int i = 0; i<inputString.length(); i++){
  13. //something here like:
  14. //outputString = outputString.concat(printPermut(inputString.slice(1))??
  15. //maybe store each unique permutation to an array or something?
  16. }
  17. }
  18. }
  19.  
  20. function permut(string) {
  21. if (string.length < 2) return string; // This is our break condition
  22.  
  23. var permutations = []; // This array will hold our permutations
  24.  
  25. for (var i=0; i<string.length; i++) {
  26. var char = string[i];
  27.  
  28. // Cause we don't want any duplicates:
  29. if (string.indexOf(char) != i) // if char was used already
  30. continue; // skip it this time
  31.  
  32. var remainingString = string.slice(0,i) + string.slice(i+1,string.length); //Note: you can concat Strings via '+' in JS
  33.  
  34. for (var subPermutation of permut(remainingString))
  35. permutations.push(char + subPermutation)
  36.  
  37. }
  38. return permutations;
  39. }
  40.  
  41. var myString = "xyz";
  42. permutations = permut(myString);
  43. for (permutation of permutations)
  44. print(permutation) //Use the output method of your choice
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement