curriculum/challenges/english/blocks/daily-coding-challenges-javascript/69162d64f96574d9bb629f01.md
Given a string, return the number of distinct permutations that can be formed from its characters.
A-Z, a-z).For example, given "abb", return 3 because there's three unique ways to arrange the letters: "abb", "bab", and "bba".
countPermutations("abb") should return 3.
assert.equal(countPermutations("abb"), 3);
countPermutations("abc") should return 6.
assert.equal(countPermutations("abc"), 6);
countPermutations("racecar") should return 630.
assert.equal(countPermutations("racecar"), 630);
countPermutations("freecodecamp") should return 39916800.
assert.equal(countPermutations("freecodecamp"), 39916800);
function countPermutations(str) {
return str;
}
function countPermutations(str) {
const freq = {};
for (const ch of str) {
freq[ch] = (freq[ch] || 0) + 1;
}
function factorial(n) {
let result = 1;
for (let i = 2; i <= n; i++) result *= i;
return result;
}
const n = str.length;
let result = factorial(n);
for (const ch in freq) {
result /= factorial(freq[ch]);
}
return result;
}