No repeats please algorithm instructions

No repeats please algorithm instructions
0.0 0


I understand what permutations are yet I still can’t understand what the goal of this challenge is.
In the list of what results should be achieved I can’t understand where those results are coming from.

In the list of permutations of “aab”, I don’t understand why each one of the three permutations is listed twice to get six.


Think of it this way. Le’ts say you had three people with their respective names a, a, and b. Person #1’s name is “a”. Person #2’s name is also “a”. Person #3’s name is “b” That is why some are listed twice. They have the same name, but they are different things(people).

For just the test case of “aab” in this challenge, it could be worded as follows:

We have three people: John Doe, John Baker, and Tom Jones

How many different ways can we arrange these three people in a straight line with the restriction that anyone with the same first name can not stand beside each other?

OR worded a little differently:

How many different ways can we arrange these three people in a straight line so that the two people named John can not stand beside each other?


These are not permutations as term is used in combinatorics. Then it could be solved very simply (you’ve already implemented the factorial operation in an earlier challenge). The fact that each letter must be treated as unique (as @rmdawson71 explained) is what makes this one a beast.

If it’s any consolation, this one really took me an embarrassingly long time to figure out.


At one point I put a lot of comments in my solution for another camper, so I’m going to include that solution here for anyone who wants to go looking for a way to solve this.

/* First pass at No Repeats Please
 * Creates an array of all permutations then filters out strings with repeated letters

function permAlone(str) {
  if (typeof str !== 'string') return undefined; // invalid input
  var charArray = str.split('');
  var permArray = generatePerms('', charArray);
  permArray = permArray.filter(function(val) {
    // For more information about this look at 
    // and 
	return !(/(\w)\1/.test(val));
  return permArray.length;

// Recursive function to generate all permutations (see graphic representation below)
function generatePerms(str, remaining) {
  var results = [];
  // only one letter left to add
  if (remaining.length === 1) {
	results = [str + remaining[0]];
  // recursive case
  else if (remaining.length > 1) {
	for(var i = 0; i < remaining.length; i++){
		var newRemaining = remaining.slice(); // create a copy by value
		results = results.concat(generatePerms(str + remaining[i], newRemaining));
  return results;


/////////GRAPHIC REPRESENTATION OF generatePerms//////////////////
//                   [a,b,a]
//			    / 	     |          \
//		      /	         |            \
//          /            |              \
//         |             |               
//      a[b,a]         b[a,a]          a[a,b]
//      /   \          /   \            /   \
//  ab[a]  aa[b]    ba[a]  ba[a]    aa[b]  ab[a]
//    |       |       |      |        |      |  
//   aba     aab     baa    baa      aab    aba