Affirm may compact repeated characters in a sequence used by a downstream text-processing component. Given a list of single-character strings, compress each consecutive run in place using run-length encoding.
For each run, write the character once. If its count is greater than 1, write the decimal digits of the count immediately after it. Return the length of the compressed prefix. The input list may contain unused values beyond the returned length, and the algorithm must not allocate another list proportional to the input size.
chars, a mutable list of single-character strings.chars must contain the compressed result.12 becomes "1", "2".Example 1
Input: chars = ["a", "a", "b", "b", "c", "c", "c"]
Output: 6, chars[:6] = ["a", "2", "b", "2", "c", "3"]
Each consecutive run is replaced by its character and count.
Example 2
Input: chars = ["a", "b", "c"]
Output: 3, chars[:3] = ["a", "b", "c"]
Runs of length one omit the count.
def compress(chars):