A fintech company streams millions of mobile app audit logs per minute to detect suspicious behavior. To reduce bandwidth and storage costs, the client SDK performs on-device compression before upload. Because this runs on low-memory devices and must avoid GC pressure, the compression must be done in-place.
You are given a list of characters chars representing a log payload. Compress it in-place using the following rule:
12 becomes '1','2').Return the new length of the compressed list. The first k characters of chars must contain the compressed result, where k is the returned length. You must use O(1) extra space (excluding the input array itself).
chars: list[str] where each element is a single-character stringint (the length of the compressed content)Input: chars = ['a','a','b','b','c','c','c']
Output: 6
Explanation (step-by-step):
'a' repeats 2 times → write 'a','2''b' repeats 2 times → write 'b','2''c' repeats 3 times → write 'c','3'
Compressed prefix becomes ['a','2','b','2','c','3'].Input: chars = ['a']
Output: 1
Explanation: Only one 'a', so we keep it as-is with no count.
1 <= chars.length <= 2 * 10^5chars[i] is a lowercase English letter, uppercase English letter, digit, or symbolchars in-place with O(1) extra space