Given an array of characters chars, compress it in-place using the following algorithm: Begin with an empty string s. For each group of consecutive repeating characters in chars: if length is 1, append the char; if length > 1, append char followed by length. Return the new length.
Input: chars = ["a","a","b","b","c","c","c"]
Output: 6
Explanation: Compressed to ["a","2","b","2","c","3"]
Topics: strings, two-pointers
Asked by: Amazon, Google, Microsoft, Meta
Time complexity: O(n). Space complexity: O(1).