Re: [閒聊] 每日leetcode
3163. String Compression III
## 思路
照題目作
記錄前一個字母跟累計個數
## Code
```python
class Solution:
def compressedString(self, word: str) -> str:
res = []
cnt = 0
prev = word[0]
for ch in word:
if ch != prev:
res.append(f'{cnt}{prev}')
cnt, prev = 1, ch
elif cnt == 9:
res.append(f'{cnt}{ch}')
cnt = 1
else:
cnt += 1
res.append(f'{cnt}{ch}')
return ''.join(res)
```
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 185.213.82.69 (臺灣)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1730725870.A.477.html
討論串 (同標題文章)
完整討論串 (本文為第 1073 之 1548 篇):