Magicsheet logo

Redistribute Characters to Make All Strings Equal

Easy
100%
Updated 6/1/2025

Asked by 2 Companies

Redistribute Characters to Make All Strings Equal

What is this problem about?

The Redistribute Characters to Make All Strings Equal problem asks whether you can redistribute characters freely among all strings to make every string identical. The answer is yes if and only if the frequency of every character in the combined pool is divisible by the number of strings. This easy coding problem tests frequency divisibility. The hash table, counting, and string interview pattern is demonstrated.

Why is this asked in interviews?

Amazon asks this to test the mathematical insight: if you pool all characters, each string needs total_freq[c] / n of each character c. This is only possible if every character's total frequency is divisible by n.

Algorithmic pattern used

Total frequency + divisibility check. Count total frequency of every character across all strings. For each character, check if total_freq[c] % n == 0. If all pass, return true; else false.

Example explanation

words=["abc","aabc","bc"]. n=3. Total: a=3, b=3, c=3. 3%3=0 for all → true (each string gets [a,b,c]).

words=["ab","a"]. n=2. Total: a=3, b=1. 3%2≠0 → false.

Common mistakes candidates make

  • Checking per-string character counts instead of total across all strings.
  • Forgetting to divide by n (just checking divisibility, not computing each string's share).
  • Not handling characters absent in some strings (frequency=0, 0%n=0 ✓).
  • Off-by-one: n is the number of strings, not string lengths.

Interview preparation tip

Redistribute Characters tests the "equal distribution" pattern: pooling resources and checking divisibility by the recipient count. This appears in: "divide candy equally," "split array into equal-sum parts," "distribute items evenly." The key insight is always: total_count % num_recipients must equal 0 for every resource type.

Similar Questions