# Permutation problem

hello, I am trying to solve no repeats algorithm, my code is

`````` function perMute(str){

let resultArr = [] // in this array we will save our combinations
str = str.split("");
//let count how many combination is possible(factorial of length)
let com = 1
let len = str.length;
for(let i =1;i<=len;i++){
com*=i
}
// looping and swaping
let places = len - 1;
let start = 0;

for(let i = 1;i<=com;i++){
swapme(str,start,start+1);
start++;
if(start === len-1){
start = 0;
} // reset the start value if it's same as str length
}

// make a swap function

function swapme(str,indexA,indexB){
let tmp = str[indexA];
str[indexA] = str[indexB];
str[indexB] = tmp;
// test the result and insert it
let testing = /(.)\1+/;
if(testing.test(resultArr)){
resultArr.shift();
}
if(!testing.test(str.join(""))){
resultArr.push(str.join(""));
}
}

return resultArr.length

}
``````

for perMute(“abc”),perMute(“aab”) the result is correct
however it didn’t return correct result in perMute(“abcdefa”)
please someone tell what i did wrong in this code

Ok, brute force checking of all possibilities is possible but tricky. I think that your logic for generating all of the possible permutations is a bit off.

Can you describe to me, in words, how you should make every single permutation?

@JeremyLT thank you for your reply. I will try to describe it but sorry for my bad english

let’s take a string “abc”. this string has 6 combination.
I use n! formula to calculate number of combination.
n = length of string. “com” save the number of combination. the 6 combination is “abc”, “bac” , “bca”, “cba”, “cab” ,“acb”. every time the first letter it’s place. in “abc” first letter is “a” which change it’s position 2 times. abc > bac > bca. So, number of positon is string.length - 1. I save this value in place variable. I use swapMe function for swapping the position of letter. create a for loop. in every loop it will call the swapMe function.

Ok, 2 things

1. I don’t see where you swap each string value to the start.
2. I am not sure what you are doing with that test in the swap function.