You want to keep track of a read/write pointer, as well as the streak of current characters. Then, after counting the run of the current pointer, you have to go back to the previous pointer and then encode the grouping, making sure to encode each number as its separate digits for some reason.
class Solution:
def compress(self, chars: List[str]) -> int:
write = 0 # where we write compressed output
read = 0 # current position while scanning
while read < len(chars):
char = chars[read]
count = 0
# count the run of the same character
while read < len(chars) and chars[read] == char:
read += 1
count += 1
# write the character
chars[write] = char
write += 1
# write the count if > 1
if count > 1:
for c in str(count):
chars[write] = c
write += 1
return write