No repeats please...I'm about to break down

Hi All,
I’m a french woman who strated Freecodecamp since 8 months with a break of 2-3 months and i’m taking my head on this challenge since ten days.
I can’t moving on while i don’t manage this challenge.
I’ve seen solution on the web, but i can’t tell myself to put a solution that I don’t understand.
But now, I wanna cry of despair.
Is anyone nice who can take the time to explein me how the solution works?
I try a lot of things by myself but they didn’t work, and seen solutions but i don’t understand them.
I don’t want to drop now.

By example, this is the last thing I try :

 function permAlone(str) {

 var count=0;
 permutations=[];
 var newTab,
 reg= /(.)\1+/g;
 var tmp;



 for (var i=0; i<str.length; i++){

  var toPermute=str.substring(i,-1)+str.substring(i+1);
  console.log(toPermute);

  var valDeRef= str.substring(i,i+1);
  console.log(valDeRef);
  toPermute= toPermute.split('');

  for (var j=0; j<toPermute.length; j++){
   tmp=toPermute[0];
   toPermute[0]=toPermute[j];
   toPermute[j]=tmp;

   console.log(toPermute);
   var test = valDeRef.concat(toPermute.join(''));
   console.log(test);
   console.log(test.match(reg));
   if(!test.match(reg)){
    count++;
    console.log(count);
  }
}

}

A big thanks to who will help me

from what I can see, it takes each character in str and puts it apart, then it takes the rest of the characters and rotates them. Lastly it joins them and tests for continuous characters, if it doesn’t find any it increments the count.

So the algorithm creates all possible permutations and counts the ones that don’t have repeated characters.

I have another solution that uses a very similar method, I create all permutations and stores them in a list, then I check each element.

function permAlone(str) {
  function permute(input) {
    if(input.length === 1){
      return Array.from(input);
    }
    var permutations = [];
    var toInsert = input[0];
    permute(input.substring(1)).forEach(function(item) {
      for(var i = 0; i <= item.length; i++) {
        var newstr = item.split('');
        newstr.splice(i, 0, toInsert);
        permutations.push(newstr.join(''));
      }
    });
    return permutations;
  }

  function hasRepeat(input) {
    var str = input.split('');
    return str.reduce(function(a, e, i, arr) {
      return e === arr[i-1] ? false : a;
    }, true);
  }

  return permute(str).map(function(e) { 
    return hasRepeat(e); 
  }).filter(function(e){ 
    return e; 
  }).length;
}

Hopefully one of this might help you understand the algorithm, even if they are a bit different they are very similar in how they work.

Also a video like this might help Permutations of String | Code Tutorial

it seems that there are another set of videos on youtube that explain this very in depth

1 Like

thanks a lot for your answer.
I’m going to see your links, I hope it’ll be helpful :wink:

1 Like