Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Bonfire: No repeats please
- // Instructions: Return the number of total permutations of the provided string that don't have repeated consecutive letters.
- function permAlone(str) {
- if ( /^([a-z])\1+$/.test(str) ) return 0;
- var arr = str.split("");
- var perms = [], stack = [];
- function perm() {
- if (arr.length === 0) perms.push(stack.slice());
- for (var x = 0; x < arr.length; x++) {
- var tmp = arr.splice(x, 1)[0];
- stack.push(tmp);
- perm();
- stack.pop();
- arr.splice(x, 0, tmp);
- }
- }
- perm();
- var total = 0;
- for (var x = 0; x < perms.length; x++) {
- perms[x] = perms[x].join("");
- if (!/([a-z])\1+/.test(perms[x])) total++;
- }
- return total;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement