dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
1.6 KiB
1.6 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 69162d64f96574d9bb629f01 | Challenge 116: Permutation Count | 28 | challenge-116 |
--description--
Given a string, return the number of distinct permutations that can be formed from its characters.
- A permutation is any reordering of the characters in the string.
- Do not count duplicate permutations.
- If the string contains repeated characters, repeated arrangements should only be counted once.
- The string will contain only letters (
A-Z,a-z).
For example, given "abb", return 3 because there's three unique ways to arrange the letters: "abb", "bab", and "bba".
--hints--
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);
--seed--
--seed-contents--
function countPermutations(str) {
return str;
}
--solutions--
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;
}