PIXELBANKv9.1.0
Menu

Given an array of characters, compress it in-place using consecutive counts. Single characters stay as-is, runs become char + count. Return the new length.

Output the compressed array as space-separated characters.

Example:

Input:
a,a,b,b,c,c,c
Output:
a 2 b 2 c 3
Reasoning:
  • The input array is iterated through to identify consecutive runs of characters: a,a is one run, b,b is another, and c,c,c is the last.
  • Each run is replaced by the character and its count: a,a becomes a 2, b,b becomes b 2, and c,c,c becomes c 3.
  • The compressed array elements are then output as space-separated values: a 2 b 2 c 3.
  • The new length of the compressed array is the total count of elements, which in this case is 66 (since there are 66 space-separated values), but the problem only asks for the compressed array, not the length.
  • The resulting compressed array is output as the final result.

Constraints:

  • 1 <= len(chars) <= 2000
  • chars[i] is a letter, digit, or symbol
solution.py

Test Results

0/0
Run code to see test results.