Skip to content

Latest commit

 

History

History
50 lines (35 loc) · 1.06 KB

_3163. String Compression III.md

File metadata and controls

50 lines (35 loc) · 1.06 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : November 04, 2024

Last updated : November 04, 2024


Related Topics : String

Acceptance Rate : 68.06 %


Solutions

Python

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)