Date: 2026-06-06
Time: 18:39
rearrange-characters-to-make-target-string/solution.pyThis file solves LeetCode 2287 — Rearrange Characters to Make Target String. Given a source string s and a target string, it computes the maximum number of complete copies of target that can be formed using only the characters from s (each character consumed once per copy).
maxNumberOfCopies(s, target) -> int — The sole function. It counts character frequencies in both strings, then for each character in target, computes how many times that character's demand can be satisfied by s. The answer is the minimum across all target characters — the bottleneck letter limits how many full copies you can build.
Frequency-ratio bottleneck — This is the canonical pattern for "how many X can I build from Y" problems: count supply and demand per unit, then take the minimum ratio. It's the same idea behind maximum-number-of-balloons/solution.py (how many times can you spell "balloon" from a string).
Counter as the abstraction — Rather than manually building frequency dicts, the solution leans on collections.Counter, which makes the intent immediately clear.
collections.Counter — standard library, no external deps.test_solution.py files across the repo reference it (this is an artifact of the test harness importing from solution modules uniformly, not because those tests actually test *this* function).1. Counter(s) builds a frequency map of available characters.
2. Counter(target) builds a frequency map of required characters per copy.
3. The generator scount[c] // tcount[c] for c in t_count iterates over every distinct character in target, computing how many copies that character alone could support via integer division.
4. min(...) returns the bottleneck — the character that runs out first.
target must be non-empty. If target is empty, t_count is empty and min() over an empty iterable raises ValueError.s is missing a character that target needs, scount[c] returns 0 (Counter's default), so 0 // tcount[c] yields 0 — the function correctly returns 0.None. The function trusts its caller to provide valid, non-empty strings — appropriate for a LeetCode solution where the problem constraints guarantee 1 <= target.length. An empty target would crash with ValueError from min() on an empty sequence.