Permutation Count - FreeCodeCamp Daily Challenge
Introduction
Permutation counting is a classic topic in combinatorics and frequently appears in interviews and programming challenges. In this article, we solve the “Permutation Count” problem from FreeCodeCamp, where we must calculate how many unique ways there are to reorder the characters of a string, considering possible repetitions.
Problem Statement
Given a string, return the number of distinct permutations that can be formed with its characters. If there are repeated characters, duplicate permutations should only be counted once.
Example:
- Input:
"abb" - Output:
3(“abb”, “bab”, “bba”)
Suppose you have the letters a, b, b:
The permutations “abb”, “bab”, and “bba” are unique. If you tried to list all possible permutations (3! = 6), you would notice that some are repeated due to the presence of two ‘b’s.
Mathematical Review: Permutations with Repetition
When there are repeated characters, we use the formula:
where:
- is the total number of characters
- is the frequency of each repeated character
Example:
- For “aabb”: , (‘a’), (‘b’)
Strategy and Analysis
- Count the frequency of each character
- Calculate the factorial of the total number of characters
- Calculate the product of the factorials of the frequencies
- Apply the formula for permutations with repetition
Implementation in JavaScript
function countPermutations(str) {
function factorial(n) {
if (n === 0 || n === 1)
return 1
let result = 1
for (let i = 2; i <= n; i++) {
result *= i
}
return result
}
const freq = {}
for (let char of str) {
freq[char] = (freq[char] || 0) + 1
}
const n = str.length
let numerator = factorial(n)
let denominator = 1
for (let key in freq) {
denominator *= factorial(freq[key])
}
return numerator / denominator
}Test Cases and Edge Cases
| Input | Output | Reasoning |
|---|---|---|
| ”abc” | 6 | 3! = 6 (all unique) |
| “aabb” | 6 | 4!/(2!*2!) = 6 |
| ”aaaa” | 1 | 4!/4! = 1 (all identical) |
| “abcde” | 120 | 5! = 120 (all unique) |
| "" | 1 | Only the empty string |
Complexity
- Time: , where is the length of the string and is the number of unique characters.
- Space: , for the frequency object.
Reflections and Learnings
- Using a HashMap to count frequencies is fundamental in string problems.
- The formula for permutations with repetition is key in combinatorics.
- Precomputing factorials or using memoization can optimize for long strings.