In [25]:
from typing import List
class Solution:
    def compress(self, chars: List[str]) -> int:
        read = 0
        write = 0
        n = len(chars)
        while read < n:
            char = chars[read]
            count = 0
            while read < n and chars[read] == char:
                read += 1
                count += 1
            chars[write] = char
            write += 1
            if count > 1:
                for digit in str(count):
                    chars[write] = digit
                    write += 1
        return write
In [26]:
sol = Solution()
print("Output :- ",sol.compress(["a","a","b","b","c","c","c"]))
print("Output :- ",sol.compress(["a"]))
print("Output :- ",sol.compress(["a","b","b","b","b","b","b","b","b","b","b","b","b"]))
Output :-  6
Output :-  1
Output :-  4