InterviewPrepKit

Home / Coding / Algorithm & Data Structure / Two Pointers

String Compression

medium Original ↗ 00:00

Problem

You are given a list of characters chars. Compress it in place using run-length encoding: replace each maximal run of a repeated character with the character followed by the run’s length — except that runs of length 1 get no number. Lengths of 10 or more are written as their individual digit characters (e.g. a run of 12 as becomes 'a', '1', '2').

Overwrite the front of chars with the compressed sequence and return its length. Use O(1) extra space, so you cannot build a separate output string.

Examples

Example 1

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

Runs aa, bb, ccc become a2, b2, c3.

Example 2

Input:  chars = ["a"]
Output: 1, chars = ["a"]

A single character gets no count.

Example 3

Input:  chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]
Output: 4, chars = ["a","b","1","2", ...]

a alone, then twelve bs → b followed by digits '1', '2'.

Constraints

  • 1 <= chars.length <= 2000
  • chars[i] is a lowercase/uppercase letter, digit, or symbol.
  • Must run in O(n) time and O(1) extra space. The in-place overwrite is the main difficulty.

Think about it first

Hint 1 First solve it with a separate output list: scan runs, append char + count. The in-place version is the same scan; the only question is where to put the output.
Hint 2 Use a `read` pointer to find the end of each run and a `write` pointer for the compressed output. Why is it guaranteed that `write` never overtakes `read`?
Hint 3 Because a run of length L (L ≥ 2) compresses to at most 1 + digits(L) ≤ L characters, the compressed prefix never outgrows the consumed input. Scan run by run: write the char, and if the run length is > 1, write each digit of the length with `str(length)`.

Write your solution, then hit Run tests to check it — or get a mock grade from the AI coach.

The coach remembers this session — revise your code and ask again, and it grades your progress. It gives hints, not the answer.
Report a bug