aboutsummaryrefslogtreecommitdiff
path: root/min_del_make_str_special.py
blob: 32c66e08a9a44ba99063402c166752374445275b (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
33
# 3085. Minimum Deletions to Make String K-Special
from collections import Counter

"""
you are given a string 'word' and an integer 'k'. we consider 'word' to be
k-special if '|freq(word[i]) - freq(word[j])| <= k' for all indices 'i' and
'j' in the string. here, 'freq(x)' denotes the frequency of the character 'x'
in the 'word'. and '|y|' denotes the absolute value of 'y'. return the
minimum number of characters you need to delete to make 'word' k-special.
"""


class Solution(object):
    def minimumDeletions(self, word, k):
        """
        :type word: str
        :type k: int
        :rtype: int
        """
        freq, ans = Counter(word).values(), float("inf")
        for i in freq:
            curr = 0
            for j in freq:
                curr += j if j < i else max(0, j - (i + k))
            ans = min(ans, curr)
        return ans


if __name__ == "__main__":
    obj = Solution()
    print(obj.minimumDeletions(word="aabcaba", k=0))
    print(obj.minimumDeletions(word="dabdcbdcdcd", k=2))
    print(obj.minimumDeletions(word="aaabaaa", k=2))