Description

Given an array of characters chars, compress it using the following algorithm: consecutive repeated characters should be replaced with the character followed by the count.

Examples

Input:chars = ["a","a","b","b","c","c","c"]
Output:["a","2","b","2","c","3"]
Explanation:

Compressed form.

Input:chars = ["x","x","x","x","x","x","x","x","x","x","x","x"]
Output:["x","1","2"]
Explanation:

The character 'x' appears 12 consecutive times. Since the count is greater than 9, it needs to be represented as multiple digits: '1' and '2' to form '12'.

Input:chars = ["A","B","B","1","1","1","1","@"]
Output:["A","B","2","1","4","@"]
Explanation:

Mixed character types are compressed separately: 'A' appears once (no count needed), 'B' appears twice, digit '1' appears 4 times, and symbol '@' appears once. Each group of consecutive identical characters is compressed independently.

Constraints

  • 1 ≤ chars.length ≤ 2000
  • chars[i] is a lowercase letter, uppercase letter, digit, or symbol.

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!