No repeats please and Heap's algorithm
See original GitHub issueNo repeats please
https://www.freecodecamp.com/challenges/no-repeats-please
Issue Description
I’m going nuts. Could someone please walk me through Heap’s algorithm? Why do we check int
to see if it’s odd or even and why do we determine which elements to swap based on that? Where does this odd or even idea come from? Assuming that I don’t know this algorithm, how can I solve this challenge?
function perm(str) {
// Split the string into an array of characters.
var arr = str.split('');
var permutations = [];
var tmp;
// 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) { // int is length of array
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);
if (int % 2 === 0) {
swap(i, int - 1);
} else {
swap(0, int - 1);
}
}
}
}
generate(arr.length);
return permutations;
}
console.log(perm("abcd"));
Issue Analytics
- State:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Free Code Camp (FCC) - No Repeats Please (Heap's algorithm)
A good start today will be solving Free Code Camp's No Repeats Please problem throug the use of Heap's Algorithm.
Read more >No repeats please, Heap's algorithm and frustration with ...
“No repeats please” is a hard challenge. Solve it whatever way you can first, then come back and refactor. Give yourself a break...
Read more >No repeats please and Heap's algorithm · Issue #11553 - GitHub
I'm going nuts. Could someone please walk me through Heap's algorithm? Why do we check int to see if it's odd or even...
Read more >Free Code Camp: No Repeats Please - K. Anthony
To do this, most folks went to Heap's algorithm, which is an algorithm for generating all possible permutations of a number of objects....
Read more >Heap's Algorithm for generating permutations - GeeksforGeeks
Heap's algorithm is used to generate all permutations of n objects. The idea is to generate each permutation from the previous permutation ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@fhdhsni maybe try again today?
@Mongues looks like you may have accidentally subscribed to all notifications for the FreeCodeCamp repository. To stop receiving notifications please visit the subscriptions page and select “Not watching”. Additionally, you can visit the GitHub Help page on managing notification delivery methods for more help.
We are sorry for your inconvenience!