Created
May 7, 2018 01:25
-
-
Save johncrisostomo/ea4e838b081a5f7403bd848a8a611fc9 to your computer and use it in GitHub Desktop.
my solution for 'no repeats please' on fcc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function permAlone(str) { | |
if (str.length === 1) { | |
return 1; | |
} | |
var factorial = fact(str.length); | |
var permutations = computePermutations(str.split(''), factorial); | |
var unique = permutations.filter(function (perm) { | |
return !perm.match(/(.)\1{1,}/); | |
}); | |
return unique.length; | |
} | |
function computePermutations(arr, factorial) { | |
var length = arr.length; | |
var temp; | |
var perms = []; | |
var i = 1; | |
var j = 0; | |
for (var count = 0; count < factorial;) { | |
var current = arr; | |
var k = 0; | |
while (k != factorial/length) { | |
while (i != length - 1) { | |
perms.push(arr.join('')); | |
temp = arr[i]; | |
arr[i] = arr[i + 1]; | |
arr[i + 1] = temp; | |
k += 1; | |
count += 1; | |
i += 1; | |
} | |
i = 1; | |
} | |
j += 1; | |
if (j === length) { | |
break; | |
} | |
temp = arr[0]; | |
arr[0] = arr[j]; | |
arr[j] = temp; | |
} | |
return perms; | |
} | |
function fact(n) { | |
if (n === 1) { | |
return n; | |
} | |
return n * fact (n - 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment