You get an array of characters chars. Compress it in this way.
Go through the array and find each group of equal characters that sit together. For each group:
- If the group has only 1 character, write that character.
- If the group has more than 1 character, write the character and then the size of the group.
Do not return the new string. Write it back inside chars, starting at position 0. A group size of 10 or more takes several characters. For example, a size of 12 is written as the two characters "1" and "2".
When you finish, return the new length of the part of the array you wrote.
Use only constant extra space.
How this judge checks it. It reads two things after your function ends: the new length k that you return, and the first k places of chars. Places after the first k are not checked. In the test results the answer is written as [k, [the first k places of chars]]. For example 1 it is [5,["x","3","y","z","2"]].
Examples
Example 1:
Input: chars = ["x","x","x","y","z","z"]
Output: 5
Explanation: The groups are "xxx", "y" and "zz". They are written as "x3yz2", which has 5 characters. The first 5 places of chars become ["x","3","y","z","2"].
Example 2:
Input: chars = ["q"]
Output: 1
Explanation: There is one group with one character, so it stays "q".
Example 3:
Input: chars = ["w","w","w","w","w","w","w","w","w","w","w","w","k"]
Output: 4
Explanation: The groups are twelve "w" and one "k". They are written as "w12k", which is the four characters "w", "1", "2" and "k".