Compress String (Run-Length Encoding)
Run-Length Encoding / Two PointersSolved Oct 31, 2025
Go step by step: try it yourself before the video and the visual. Progress saves on this device.
0 / 130 pointsSolved (Medium) 0/40Reviews 0/50Mastered 0/25Practice steps 0/15
Step 1
Know these before you start
Practise each one here, or tick what you already know
Watch the concept video if the pattern is new to you.
Step 2
Try it on paper
5–10 minutes, no code yet
ProblemProblem statementGiven a string S composed of repeating characters, compress it using Run-Length Encoding (RLE). The compressed string should consist of each unique character followed by the count of its consecutive occurrences. (e.g., "AAABBC" becomes "A3B2C1").ReadHide
Problem Description
Given a string S composed of repeating characters, compress it using Run-Length Encoding (RLE). The compressed string should consist of each unique character followed by the count of its consecutive occurrences. (e.g., "AAABBC" becomes "A3B2C1").
Code it here
Tab / Shift+Tab indent · ⌘/Ctrl+/ comments · ⌘/Ctrl+Enter runs · Esc leaves the editor
Step 6
Get it accepted
All test cases pass
Step 7
Review it so it sticks
Re-solve from memory after
The step that matters most: without reviews most of a solved problem fades within weeks. Three short re-solves, each just as you start to forget, are what make it yours.
Your notes
Three lines in your own words. They come back with this problem's reviews, so you can check yourself after recalling the solution.
Solve it yourself before you look
Reading the solution first feels like progress, but it makes the next similar problem — and the interview version — much harder, because you skipped the part where you figure it out. Give it an honest 20–30 minutes. Stuck? Re-read the pattern, watch the concept video, or try the brute force first.
Hidden: approach · solution code · optimal solution
Approach
Use a two-pointer approach (read pointer and write pointer) to count consecutive identical characters and replace the sequence with the character followed by the count.
Approaches
1. Two-Pointer Run-Length Encoding (Optimal) - O(N) ⭐
Idea: Use a single pass where one pointer (read) scans ahead to count the current run, and the result is constructed sequentially.
- Initialize a
resultbuilder/string. - Use an outer loop index (
i) to track the start of the current run. - Use an inner loop index (
j) to find the end of the run (whereS[j]is different fromS[i]). - Append
S[i]and the count(j - i)to the result. - Set
i = jto start the next run.
Time: O(N)
Space: O(N) (for the result string).
Solution code
import sys
def solve_compress_string():
try:
S = sys.stdin.readline().strip()
except:
return
if not S:
print("")
return
# Optimal: O(N) Time, O(N) Space - Single Pass with Counting
compressed = []
i = 0
N = len(S)
while i < N:
current_char = S[i]
count = 0
j = i
# Count the consecutive run
while j < N and S[j] == current_char:
count += 1
j += 1
# Append character and count
compressed.append(current_char)
compressed.append(str(count))
# Move i to the start of the next run
i = j
print("".join(compressed))
# Brute Force is effectively the same logic (O(N))
def solve_compress_string_brute():
solve_compress_string()
if __name__ == "__main__":
solve_compress_string()Other ways to solve it
- Thought of another approach (slower, or cleverer)? Code it here: the same tests check any approach.
Revising this one?
- →Solve it from memory before peeking
- →Say the pattern out loud, not just the code
- →Explain it like you would to a friend
- →Try it again in a different language