File: redistribute-characters-to-make-all-strings-equal/solution.py

Date: 2026-06-06

Time: 18:41

Purpose

This file solves LeetCode 1897: Redistribute Characters to Make All Strings Equal. It determines whether you can redistribute characters freely across all strings in a list so that every string ends up identical. It owns a single pure function with no side effects.

Key Components

redistributecharacterstomakeallstringsequal(words: list[str]) -> bool

The sole public function. Contract: given a list of lowercase English strings, return True if and only if the characters can be redistributed so all strings become equal.

The algorithm:

1. Count every character across all strings combined.

2. For all strings to be identical, each character's total count must divide evenly by n (the number of strings). If any character has a count that isn't divisible by n, redistribution is impossible.

Patterns

Dependencies

Flow


words → len(words) = n
      → Counter() accumulates char frequencies across all words
      → all(count % n == 0 for each character count) → bool

The entire function is a single pass over all characters (O(total chars)) plus one pass over the counter (O(unique chars)).

Invariants

Error Handling

None — the function trusts its input matches the LeetCode constraint (non-empty list of lowercase strings). No validation, no exceptions. This is appropriate for a contest solution where inputs are guaranteed valid.

Topics to Explore

Beliefs