9. String Compression
Compress the list of characters chars in place and return the new length.
Each run of the same character becomes that character, followed by the run length if
the run is longer than 1. For example ["x","x","x","y"] becomes ["x","3","y"]. A
length of 10 or more is written as separate digit characters (12 becomes "1","2").
Write the result into the start of chars and return its length k. Only chars[:k]
is checked. Aim for O(1) extra space.
Constraints
1 <= len(chars) <= 2000chars[i]is a letter, digit or symbol.
Examples
Example 1
Input: chars = ["x","x","x","y"] Output: ["x","3","y"]
Example 2
Input: chars = ["q"] Output: ["q"]
Example 3
Input: chars = ["m","n","n","n","n","n","n","n","n","n","n","n","n"] Output: ["m","n","1","2"]
Hints
Hint 1
Use a read pointer to find each group and a write pointer to overwrite chars in place.
Hint 2
Counts of 10 or more take several characters: write str(count) one digit at a time.