Skip to content
Gallery
Algoschool
Share
Explore
Level 1

icon picker
Arrays problems



class Solution:
def compress(self, chars: List[str]) -> int:
prev = chars[0]
count = 0
j = 0
for i in range(len(chars)):
if chars[i] == prev:
count += 1
else:
chars[j] = prev
j += 1
if count > 1:
digits = str(count)
for k in range(len(digits)):
chars[j] = digits[k]
j += 1

prev = chars[i]
count = 1

chars[j] = prev
j += 1
if count > 1:
digits = str(count)
for k in range(len(digits)):
chars[j] = digits[k]
j += 1
return j

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.