Skip to content

Instantly share code, notes, and snippets.

@LionRoar
Created February 2, 2018 20:12
Show Gist options
  • Save LionRoar/1acdcd87eb09d7316d5935100366f8f2 to your computer and use it in GitHub Desktop.
Save LionRoar/1acdcd87eb09d7316d5935100366f8f2 to your computer and use it in GitHub Desktop.
NoRepeatsPlease freecodecam solutions
function permAlone(str) {
// Create a regex to match repeated consecutive characters.
var regex = /(.)\1+/g;
// Split the string into an array of characters.
var arr = str.split('');
var permutations = [];
var tmp;
// Return 0 if str contains same character.
if (str.match(regex) !== null && str.match(regex)[0] === str) return 0;
// Function to swap variables' content.
function swap(index1, index2) {
tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}
// Generate arrays of permutations using the algorithm.
function generate(int) {
if (int === 1) {
// Make sure to join the characters as we create the permutation arrays
permutations.push(arr.join(''));
} else {
for (var i = 0; i != int; ++i) {
generate(int - 1);
swap(int % 2 ? 0 : i, int - 1);
}
}
}
generate(arr.length);
// Filter the array of repeated permutations.
var filtered = permutations.filter(function(string) {
return !string.match(regex);
});
// Return how many have no repetitions.
return filtered.length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment