blob: c76b9b2f2c195c28cf3dfbeaee8ddb73bc5a85f4 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# 1897. Redistribute Characters to Make All Strings Equal
from collections import defaultdict
"""
given an array of strings 'words' in one operation, pick two distinct indices
'i' and 'j' where 'words[i]' is a non empty string and ove any character from
words[i] to any position in 'words[j]'. return 1 if you can make every string
in 'words' equal using any number of operations, and 0 otherwise.
"""
class Solution(object):
def makeEqual(self, words):
"""
:type words: List[str]
:rtype: bool
"""
cnt = defaultdict(int)
for w in words:
for c in w:
cnt[c] += 1
n = len(words)
for val in cnt.values():
if val % n != 0:
return False
return True
if __name__ == "__main__":
obj = Solution()
print(obj.makeEqual(words=["abc", "aabc", "bc"]))
print(obj.makeEqual(words=["ab", "a"]))
|