No Repeats Please (Help Requested)

No Repeats Please (Help Requested)
0

#1

Been at this one for awhile but I can’t satisfy 2 of the test cases.

abcdefa getting back 3120, should be 3600
abfdefa getting back 2304, should be 2640

I know there are likely many ways to optimize this code, but I’d like to get what I have working as it’s something I (mostly) understand. Any suggestions are much appreciated :slight_smile:

function permAlone(str) {

  var allPermutations = permutator(str.split(''));
  var output = [];
  
  // loop to remove any permutation with a duplicate from the array
  for (var i = 0; i < allPermutations.length; i++) {
  	if (!hasRepeatingCharacter(allPermutations[i])) {
      output.push(allPermutations[i]);
    }	
  }
  
  // return the length of array
  return output.length;
}

function permutator(inputArr) {
  var results = [];

  function permute(arr, memo) {
    var cur, memo = memo || [];

    for (var i = 0; i < arr.length; i++) {
      cur = arr.splice(i, 1);
      if (arr.length === 0) {
        results.push(memo.concat(cur));
      }
      permute(arr.slice(), memo.concat(cur));
      arr.splice(i, 0, cur[0]);
    }

    return results;
  }

  return permute(inputArr);
}

function hasRepeatingCharacter(arr) {
	
  var testString;
  
  for (var i = 0; i < arr.length; i++) {
  	testString = testString + arr[i];
  }
  
  return (/(\w)\1+/.test(testString));
}

#2

Not sure if you still need help with this, but I would suggest console.log-ging what testString is in your hasRepeatingCharacter function and working from that.


#3

Heh, I do still need help! Thank you for the suggestion. I’ll see where it gets me :+1:t2:


#4

I got it! My problem was that I didn’t instantiate my “string” variable with a value so “undefined” was being tacked on to the front of it. I solved it by setting it to an empty string and bingo.