Today's Daily Challenge (4th December 2025)

Can someone help with today’s challenge? The challenge link: Today’s Daily Challenge

My attempt:

function countPermutations(str) {
  const count = (word = str, prefix = "", results = new Set()) => {
    if (word.length === 0) {
      results.add(prefix);

      return results.size;
    }

    for (let i = 0; i < word.length; i++) {
      let pre = prefix + word[i];
      let newStr = word.slice(0, i) + word.slice(i + 1);

      count(newStr, pre, results);
    }

    return results.size;
  };

  return count(str);
}

My code fails this test

// running tests
4. countPermutations(“freecodecamp”) should return 39916800.
// tests completed

I know why it is failing the test; the values exceed the max set size. I also tried with an array. I also tried with a numeric count that increments in the if statement, but since it still relies on the set mutation, it produces the same failure for test 4.

But I don’t know how to resolve the issue…

Hi @Bukki

Try this console log:

console.log(countPermutations("freecode"));

Happy coding

It might be not the issue with set itself, but rather an approach, which gets very slow with longer words.

For example, with "abc", count function gets called internally 16 times. For the "racecar" that number goes up to 13700.

Last test is timing out before getting anywhere near the result.

Yeah, that’s what I meant, there needs to be a way to count only unique permutations, and it seems storing it in an array or set or something is the way to access only unique permutations. If I use only an incremental count, it will count all permutations, including those that appeared before — for “racecar”, the incremental count exceeds 5000. The issue is not the set, it’s the call, but I don’t know how to resolve it…

The log returns 6720…

Hi @Bukki

When I tried it, the console triggered a potential infinite loop warning.

Try and figure out how to detect permutations (unique) and not combinations (order does not matter).

Happy coding

“freeCodeCamp” does indeed trigger an infinite loop because as I mentioned from beginning, the size exceeds the set—it probably exceeds max call stack size

Try doing a search for “javascript memoization”.

Okay. Will do… thanks