3163. String Compression III
All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : November 04, 2024
Last updated : November 04, 2024
Related Topics : String
Acceptance Rate : 68.06 %
class Solution:
def compressedString(self, word: str) -> str:
output = []
prev_char, cnt = None, 0
for c in word :
if cnt == 9 :
output.append(str(cnt) + prev_char)
prev_char, cnt = None, 0
if c == prev_char :
cnt += 1
continue
if prev_char :
output.append(str(cnt) + prev_char)
prev_char = c
cnt = 1
output.append(str(cnt) + prev_char)
return ''.join(output)