---
# System prepended metadata

title: 2268. Minimum Number of Keypresses
tags: [Leetcode, Sort, Medium, Math, Greedy]

---

Link: https://leetcode.com/problems/minimum-number-of-keypresses/description/
## 思路
将所有字母的出现次数排序
出现次数多的字母肯定要放在每个key的最前面
## Code
```python=
class Solution:
    def minimumKeypresses(self, s: str) -> int:
        count = Counter(s)
        sortedCount = sorted(count.values())[::-1]
        if len(sortedCount)<=9:
            return len(s)
        elif 9<len(sortedCount)<=18:
            return sum(sortedCount[:9])+2*sum(sortedCount[9:])
        else:
            return sum(sortedCount[:9])+2*sum(sortedCount[9:18])+3*sum(sortedCount[18:])
```