I realized that I didn’t need the “count” variable so I removed it and the loop too. Here’s the updated code:
function countPermutations(str) {
const letters = {};
const letterCounts = {};
const arr = [];
let counter = 0;
str = str.split("");
for (const curr of str){
if (letters[curr] == undefined){
letters[curr] = 0;
letterCounts[curr] = 1;
continue;
}
letterCounts[curr]++;
}
const copy = {...letters};
permutate(str.length, "", copy, 0);
function permutate(n, curr, object, c){
if (arr.indexOf(curr) !== -1) return;
if (c === n){
arr.push(curr);
counter++;
return;
}
const keys = Object.keys(object);
keys.forEach((letter) => {
console.log(object[letter] !== letterCounts[letter])
if (object[letter] !== letterCounts[letter]){
object[letter]++;
permutate(n, curr + letter, object, c + 1);
}
})
}
console.log(arr);
return counter;
}
console.log(countPermutations("abb"));
Here’s what the function is supposed to do:
takes string and turns it into an array
makes two objects:
the first object saves all the possible letters with a counter always set to 0
the second object saves the number of occurences for the letters
initializes the counter for the tests to succeed
starts the “permutate” function which takes 4 arguments:
the first one is the length of the string which was input in the “countPermutations” function
the second one is the current permutation, e. g. “ab”, “bb”, whatever
the third one is the first object, which will increment as it goes through the recursion
the fourth one will be the counter for the current permutation,
when the permutate function starts, it checks if the current permutation is already inside the array, if it is it immediatly exits out
it then checks if the 1st and 4th argument are the same number, aka if the permutation now has the length of the input array. if it does it pushes it into the array to save it for the condition before, and it also increments the counter.
then the function saves the object keys for the loop
the loop checks, if the current letter already has used up all of its instances, if it doesn’t it increments the object to use it again when the function calls itself.
but then the loop doesn’t continue, when I call the function and I don’t know why
I askes Sokrates and he gave me the hint, that I had to decrement the value after the recursion call, this finally made the code work for tests 1-3, but now the problem is, that the code times out before it finished for the final test.
Here’s the updated code:
function countPermutations(str) {
const letters = {};
const letterCounts = {};
let counter = 0;
str = str.split("");
for (const curr of str){
if (letters[curr] == undefined){
letters[curr] = 0;
letterCounts[curr] = 1;
continue;
}
letterCounts[curr]++;
}
permutate(str.length, "", letters, 0);
function permutate(n, curr, object, c){
if (c === n){
counter++;
return;
}
Object.keys(object).forEach((letter) => {
if (object[letter] !== letterCounts[letter]){
object[letter]++;
permutate(n, curr + letter, object, c + 1);
object[letter]--;
}
})
}
return counter;
}
When I ran the code in VSCode, it gave me the correct result, so I don’t know what I can do to improve my code for the online IDE.
That’s what happened to me when I did this challenge. I had to get online help to pass the tests on this one. I looked up “javascript count unique permutations of a string efficiently” to get at a solution that uses factorial math and doesn’t require saving the permutations to an array.