Strings

String Compression

medium
Given an array of characters `chars`, compress it using run-length encoding in-place. Groups of consecutive repeating characters are replaced with the character followed by the count (omit count if 1). Digits of multi-digit counts are written as separate characters. Return the new length after compression. Modify `chars` in-place for the first returned length characters. Example: `compress(["a","a","b","b","c","c","c"])` → length `6`, chars begin `["a","2","b","2","c","3"]`.
Hint steps(0/3 revealed)
  1. 1

    Complete previous step or reveal to unlock this hint.

  2. 2

    Complete previous step or reveal to unlock this hint.

  3. 3

    Complete previous step or reveal to unlock this hint.