[LeetCode] Redistribute Characters to Make All Strings Equal

1897. Redistribute Characters to Make All Strings Equal

You are given an array of strings words (0-indexed).

In one operation, pick two distinct indices i and j, where words[i] is a non-empty string, and move any character from words[i] to any position in words[j].

Return true if you can make every string in words *equal using any number of operations, and false otherwise*.

1
2
3
4
5
6
7
8
9
10
11
class Solution {
public:
bool makeEqual(vector<string>& words) {
vector<int> freq(26);
for(auto& w : words) for(auto& ch : w) freq[ch-'a'] += 1;
for(auto& x : freq) {
if(x % words.size()) return false;
}
return true;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/12/30/PS/LeetCode/redistribute-characters-to-make-all-strings-equal/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.