[LeetCode]1897. Redistribute Characters to Make All Strings Equal (Easy) Python-2023.12.30
Posted on 2023-12-30 21:21 Uriel 閱讀(38) 評(píng)論(0) 編輯 收藏 引用 所屬分類: 閑來無事重切Leet Code 、大水題 、Hash一個(gè)words集合包含若干字符串,每次操作可以將其中一個(gè)字符串的某個(gè)字符插入另一個(gè)字符串,問是否可以經(jīng)過無限次操作使得每個(gè)字符串變成相同的。直接判斷是否每個(gè)字符的總出現(xiàn)次數(shù)可以被字符串?dāng)?shù)量整除即可
1 #1897
2 #Runtime: 67 ms (Beats 16.67%)
3 #Memory: 13.6 MB (Beats 83.33%)
4
5 class Solution(object):
6 def makeEqual(self, words):
7 """
8 :type words: List[str]
9 :rtype: bool
10 """
11 total = ''.join(words)
12 chars = set(total)
13 for ch in chars:
14 if total.count(ch) % len(words) != 0:
15 return False
16 return True
2 #Runtime: 67 ms (Beats 16.67%)
3 #Memory: 13.6 MB (Beats 83.33%)
4
5 class Solution(object):
6 def makeEqual(self, words):
7 """
8 :type words: List[str]
9 :rtype: bool
10 """
11 total = ''.join(words)
12 chars = set(total)
13 for ch in chars:
14 if total.count(ch) % len(words) != 0:
15 return False
16 return True