aboutsummaryrefslogtreecommitdiff
path: root/min_del_make_str_special.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-06-20 23:27:41 -0600
committerJack Sangdahl <[email protected]>2025-06-20 23:27:41 -0600
commitab3ffb04a9356621bf2ed0e535054095e27d312d (patch)
tree6cd68af1f3dd07f76bcbee6ad661f855c5893cc2 /min_del_make_str_special.py
parenta032c2f9c7e06b0402ba2675f96830a0bf80b71e (diff)
downloadleetcode-ab3ffb04a9356621bf2ed0e535054095e27d312d.tar.gz
leetcode-ab3ffb04a9356621bf2ed0e535054095e27d312d.zip
3085. minimum deletions to make string k-special
Diffstat (limited to 'min_del_make_str_special.py')
-rw-r--r--min_del_make_str_special.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/min_del_make_str_special.py b/min_del_make_str_special.py
new file mode 100644
index 0000000..32c66e0
--- /dev/null
+++ b/min_del_make_str_special.py
@@ -0,0 +1,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))