給出一堆字符,問最少去掉幾個(gè)字符可以保證每種字符出現(xiàn)的次數(shù)不同,用python的counter統(tǒng)計(jì)次數(shù),然后用set記錄已經(jīng)出現(xiàn)過的字符頻次,若有沖突,不斷去掉字符直至不沖突
1 #1647
2 #Runtime: 274 ms (Beats 29.54%)
3 #Memory: 14.2 MB (Beats 87.50%)
4
5 class Solution(object):
6 def minDeletions(self, s):
7 """
8 :type s: str
9 :rtype: int
10 """
11 ans = 0
12 fq_set = set()
13 cnt = collections.Counter(s)
14 for ch, fq in cnt.items():
15 while fq and fq in fq_set:
16 fq -= 1
17 ans += 1
18 fq_set.add(fq)
19 return ans