String Compression

Medium · Arrays & Strings · about 15 minutes

Asked at Amazon, Microsoft, Google, Meta, Bloomberg, Salesforce, Intuit, Okta, HubSpot.

Compress the character array chars in place: each run of the same character becomes the character, followed by the run length if it's greater than 1 (a length of 10 or more is written as several digit characters). Return the new length. Only the returned length is checked here; still write the compressed characters to the front of chars.

The array is given as a string array of single characters.

Related guides