443. String Compression
Asked at Salesforce, Goldman Sachs, Anduril, Pinterest
Difficulty
Medium
Time
O(n)
Space
O(1)
Pattern
Two Pointers
Companies
4
Problem
You are given an array of characters chars. Compress it in place using this rule: look at each group of consecutive equal characters. If a group has only one character, write just that character. If a group has more than one character, write the character followed by the group's size as digits.
Do not return a new string. Instead, overwrite the front of the input array chars with the compressed result, and return the new length. Anything in the array past that length is ignored.
Counts of 10 or more take several positions. For example, a group of 12 identical characters becomes the character plus "1" and "2". You must use only constant extra space, so you cannot build a separate output array.
Groups become character plus count
Constraints
- ●
1 <= chars.length <= 2000 - ●
chars[i] is a lowercase English letter, an uppercase English letter, a digit, or a symbol - ●
The algorithm must use O(1) extra space
Examples
chars = ["a","a","b","b","c","c","c"]6chars = ["a"]1chars = ["a","b","b","b","b","b","b","b","b","b","b","b","b"]4Asked At
4 companies| Company | Difficulty | |
|---|---|---|
| Salesforce | Medium | View all Salesforce questions → |
| Goldman Sachs | Medium | View all Goldman Sachs questions → |
| Anduril | Medium | View all Anduril questions → |
| Medium | View all Pinterest questions → |
How to Think About It
Brute force: build a new string by scanning and counting consecutive characters.
Two pointers approach: use a read pointer to scan and a write pointer to compress in-place.
For each group, write the character, then write the count as individual digits if count > 1.
Handle multi-digit counts (e.g., 12 becomes characters "1" and "2").
Optimal: use two pointers with O(1) extra space, writing compressed characters as you scan.
Optimal Approach
Use two pointers: a read pointer i scans through the array, while a write pointer j marks where to write. For each group of identical characters starting at i, count the length of the group. Write the character at position j, then write the count digits if count > 1. Advance i past the group and continue. The write pointer j at the end gives the compressed length. For multi-digit counts like 12, write "1" then "2" at consecutive positions.
Read pointer and write pointer
What Trips People Up in Real Interviews
Clarify the compression format: only append count when count >= 2.
For counts >= 10, write each digit separately (not the number itself).
The compressed array must be modified in-place using the original array.
Edge case: a single character group with count 1 writes only the character.
Return the new length, not the compressed string itself.
Solution Code
O(n) timedef compress(chars):
write = 0
read = 0
n = len(chars)
while read < n:
char = chars[read]
count = 0
while read < n and chars[read] == char:
read += 1
count += 1
chars[write] = char
write += 1
if count > 1:
for digit in str(count):
chars[write] = digit
write += 1
return writeFrequently Asked Questions
What is the String Compression problem?
Given a character array chars, compress it in-place using the following rule: groups of consecutive identical characters are replaced by the character followed by the count of repetitions. The count is only written if greater than 1. Return the new length of the compressed array.
How do you solve String Compression?
The optimal approach runs in O(n) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask String Compression?
String Compression is asked at Salesforce, Goldman Sachs, Anduril, Pinterest. It is a medium difficulty problem.
What are common mistakes on String Compression?
- Clarify the compression format: only append count when count >= 2.
- For counts >= 10, write each digit separately (not the number itself).
- The compressed array must be modified in-place using the original array.
- Edge case: a single character group with count 1 writes only the character.
- Return the new length, not the compressed string itself.