Skip to content

Commit daf8dfb

Browse files
committed
Jun 21
1 parent bb06dc0 commit daf8dfb

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from collections import Counter
2+
3+
4+
class Solution:
5+
def minimumDeletions(self, word: str, k: int) -> int:
6+
freqs = list(Counter(word).values())
7+
freqs.sort()
8+
9+
min_deletions = len(word)
10+
prefix_sum = 0
11+
for i in range(len(freqs)):
12+
deletions = prefix_sum
13+
for j in range(i + 1, len(freqs)):
14+
if freqs[j] > freqs[i] + k:
15+
deletions += freqs[j] - (freqs[i] + k)
16+
min_deletions = min(min_deletions, deletions)
17+
prefix_sum += freqs[i]
18+
return min_deletions
19+
20+
21+
def main():
22+
word = 'aabcaba'
23+
k = 0
24+
assert Solution().minimumDeletions(word, k) == 3
25+
26+
word = 'dabdcbdcdcd'
27+
k = 2
28+
assert Solution().minimumDeletions(word, k) == 2
29+
30+
word = 'aaabaaa'
31+
k = 2
32+
assert Solution().minimumDeletions(word, k) == 1
33+
34+
35+
if __name__ == '__main__':
36+
main()

2025-06-June-LeetCoding-Challenge/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
| June 18 | [2966. Divide Array Into Arrays With Max Difference](https://leetcode.com/problems/divide-array-into-arrays-with-max-difference/) | Medium | Solved |
2525
| June 19 | [2294. Partition Array Such That Maximum Difference Is K](https://leetcode.com/problems/partition-array-such-that-maximum-difference-is-k/) | Medium | Solved |
2626
| June 20 | []() | | |
27-
| June 21 | []() | | |
27+
| June 21 | [3085. Minimum Deletions to Make String K-Special](https://leetcode.com/problems/minimum-deletions-to-make-string-k-special/) | Medium | Solved |
2828
| June 22 | []() | | |
2929
| June 23 | []() | | |
3030
| June 24 | []() | | |
@@ -40,5 +40,5 @@
4040
| Level | Problems | Solved | Unsolved |
4141
| --- | --- | --- | --- |
4242
| Easy | 3 | 3 | 0 |
43-
| Medium | 9 | 6 | 3 |
43+
| Medium | 10 | 7 | 3 |
4444
| Hard | 5 | 2 | 3 |

0 commit comments

Comments
 (0)