Advertisement
Guest User

Untitled

a guest
Apr 7th, 2015
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Bonfire: No repeats please
  2. // Instructions: Return the number of total permutations of the provided string that don't have repeated consecutive letters.
  3.  
  4.  
  5. function permAlone(str) {
  6.  
  7.   if ( /^([a-z])\1+$/.test(str) ) return 0;
  8.  
  9.   var arr = str.split("");  
  10.  
  11.   var perms = [], stack = [];
  12.  
  13.   function perm() {
  14.    
  15.     if (arr.length === 0) perms.push(stack.slice());
  16.    
  17.     for (var x = 0; x < arr.length; x++) {
  18.      
  19.       var tmp = arr.splice(x, 1)[0];
  20.       stack.push(tmp);
  21.       perm();
  22.       stack.pop();
  23.       arr.splice(x, 0, tmp);
  24.     }    
  25.   }
  26.  
  27.   perm();
  28.  
  29.   var total = 0;
  30.   for (var x = 0; x < perms.length; x++) {
  31.     perms[x] = perms[x].join("");
  32.     if (!/([a-z])\1+/.test(perms[x])) total++;    
  33.   }
  34.  
  35.   return total;  
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement