3545. 不同字符数量最多为 K 时的最少删除数
题目来源:
LeetCode题目:3545. 不同字符数量最多为 K 时的最少删除数 - 力扣(LeetCode)
解题思路:
保留出现次数最多的 k 个字符,删去其余的即可。
解题代码:
#python3
class Solution:def minDeletion(self, s: str, k: int) -> int:cnt=[0]*26for each in s:cnt[ord(each)-97] += 1cnt.sort(reverse=True)res=0for i in range(len(cnt)):k=k-1if k<0:res=res+cnt[i]return res
总结:
无官方题解。