Redistribute Characters to Make All Strings Equal - Problem
Imagine you're a librarian with a collection of books, and you want to reorganize the letters in their titles to make all book titles identical. You have the power to move any character from one book title to any position in another book title.
Given an array of strings words, you need to determine if it's possible to make all strings equal by redistributing characters between them. In each operation, you can:
- Pick two different strings (at indices
iandj) - Remove any character from
words[i](as long as it's not empty) - Insert that character at any position in
words[j]
Return true if you can make all strings identical, false otherwise.
Example: ["abc", "aabc", "bc"] can become ["abc", "abc", "abc"] by moving one 'a' from the second string to the third string.
Input & Output
example_1.py — Basic redistribution
$
Input:
["abc", "aabc", "bc"]
›
Output:
true
💡 Note:
We have 3 a's, 3 b's, and 3 c's total. Since 3 is divisible by 3 (number of strings), each string can have exactly 1 of each character, making them all equal to "abc".
example_2.py — Impossible redistribution
$
Input:
["ab", "a"]
›
Output:
false
💡 Note:
We have 2 a's and 1 b total. With 2 strings, we'd need an even number of each character. Since we have an odd number of b's (1), it's impossible to distribute evenly.
example_3.py — Single string edge case
$
Input:
["a"]
›
Output:
true
💡 Note:
With only one string, no redistribution is needed - it's already equal to itself.
Constraints
- 1 ≤ words.length ≤ 100
- 0 ≤ words[i].length ≤ 100
- words[i] consists of lowercase English letters only
- Sum of all string lengths ≤ 104
Visualization
Tap to expand
Understanding the Visualization
1
Count Total Chips
Count all chocolate chips across all conveyor belts
2
Check Distribution
Verify if chips can be evenly distributed among all belts
3
Redistribute
If possible, each belt gets the same pattern of chips
Key Takeaway
🎯 Key Insight: If every character count is divisible by the number of strings, perfect redistribution is always possible!
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code