PermAlone challenge

Tell us what’s happening:
Describe your issue in detail here.
I am unable to pass the No repeats pleasechallenge. The function that I have written to get all permutations of a given string is returning an array of permutations of a string where each item in an array is a string. For example perm(‘aabb’) returns an array of length 24. But upon filtering the array returned by function ‘perm’, I am unable to get the correct filtered array. Upon filtering perm(‘aabb’), for example, I am getting [‘baba’, ‘baba’, ‘baba’] as a result. According to the tests, it must contain 8 items. Can someone please help>

  **Your code so far**

function permAlone(str) {
let result=perm(str)
let regex = /(.)\1+/;
const filtered = result.filter(function(string) {
  return !string.match(regex);
});
console.log(filtered)
return filtered.length
}

/*This function removes the first letter of the string and find permutations of the remaining letters recursively. After finding the permuatations of remaining letters, it inserts first letter at different positions to generate all possible permutations*/
function perm(s){
if(s.length<2){
  return [s]
}
let result = perm(s.slice(1))
let temp=[...result]
for (let i=0;i<result[0].length;i++){
  result.push(...temp)
}
for (let i=0;i<result.length;i++){
  result[i]=result[i].slice(0,i)+s[0]+result[i].slice(i)
}
return result
}
permAlone("aabb")
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:100.0) Gecko/20100101 Firefox/100.0

Challenge: No Repeats Please

Link to the challenge:

While your perm function does return an array of length 24 (the right number), most of those items are ‘bbaa’… so I dont’ think its your filter thats the issue, it appears to be that the 24 results your getting from perm() are mostly duplicated. It never contains any permutation of ‘abab’.

I have to go, but hope that points you in the right direction.

Thanks for responding. I managed to fix my function.

result[i]=result[i].slice(0,i)+s[0]+result[i].slice(i)

Above line was the culprit.
Correct statement is

result[i]=result[i].substring(0, Math.floor(i/ factorial(result[i].length)))
+s[0]
+result[i].substring(Math.floor(i/ factorial(result[i].length)))

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.