作者dont (dont)
標題Re: [閒聊] 每日leetcode
時間2024-11-04 21:11:03
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-website.tw), 來自: 185.213.82.69 (臺灣)
※ 文章網址: https://ptt-website.tw/Marginalman/M.1730725870.A.477