String Compression - Problem
String Compression Challenge - Transform Data Efficiently!

You're given a character array chars and need to compress it using a run-length encoding algorithm. The goal is to replace consecutive duplicate characters with the character followed by its count.

The Rules:
• If a character appears only once, just keep the character
• If a character repeats consecutively, write the character + count
• For counts ≥ 10, split digits into separate array positions
• Modify the input array in-place using constant extra space
• Return the new compressed length

Example: ['a','a','b','b','c','c','c'] becomes ['a','2','b','2','c','3']

This is a classic two-pointers problem that tests your ability to manipulate arrays efficiently while managing edge cases like multi-digit counts.

Input & Output

example_1.py — Basic Compression
$ Input: chars = ['a','a','b','b','c','c','c']
Output: 6, chars = ['a','2','b','2','c','3']
💡 Note: Groups of consecutive characters: 'aa' becomes 'a2', 'bb' becomes 'b2', 'ccc' becomes 'c3'. The compressed length is 6.
example_2.py — Single Characters
$ Input: chars = ['a']
Output: 1, chars = ['a']
💡 Note: Single character 'a' stays as 'a' since groups of length 1 don't need count notation.
example_3.py — Multi-digit Count
$ Input: chars = ['a','b','b','b','b','b','b','b','b','b','b','b','b']
Output: 4, chars = ['a','b','1','2']
💡 Note: Single 'a' stays as 'a'. Twelve consecutive 'b's become 'b12'. Count 12 is split into digits '1' and '2'. Total compressed length is 4.

Visualization

Tap to expand
Book ABook ABook BBook BBook CBook COriginal Bookshelf👁SCANNERORGANIZERA2B2C2Compressed BookshelfCompression Process1. Scanner counts: A,A → 2 books2. Organizer writes: A + tag '2'3. Continue: B,B → B + '2'4. Finally: C,C → C + '2'Result: 6 → 6 (no compression gain)Transform
Understanding the Visualization
1
Setup Pointers
Place a 'scanner' pointer to read books and a 'organizer' pointer to write the compressed format
2
Count Groups
Scanner counts consecutive identical books while organizer waits
3
Write Compressed
Organizer writes the book type and count tag at the current position
4
Continue Process
Repeat until scanner reaches the end, organizer position gives final length
Key Takeaway
🎯 Key Insight: The write pointer always stays behind the read pointer, allowing safe in-place modification while maintaining the compressed format efficiently!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass through the array where n is the length of input array

n
2n
Linear Growth
Space Complexity
O(1)

Only using a constant amount of extra variables (pointers and counters)

n
2n
Linear Space

Constraints

  • 1 ≤ chars.length ≤ 2000
  • chars[i] is a lowercase English letter, uppercase English letter, or digit
  • Follow-up: Could you solve it using only O(1) extra space?
Asked in
Microsoft 45 Amazon 38 Google 32 Adobe 28
62.0K Views
High Frequency
~18 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen