Run-Length Compression
Programming Interview
Hard
5 views
Problem Description
Input provides {x}. Example aaabb becomes a3b2. Output compressed string.
Input Format
One string s.
Output Format
Compressed string.
Official Solution
import sys
s=sys.stdin.read().strip()
if not s: sys.exit(0)
out=[]
cur=s[0]
cnt=1
for ch in s[1:]:
if ch==cur:
cnt+=1
else:
out.append(cur+str(cnt))
cur=ch
cnt=1
out.append(cur+str(cnt))
sys.stdout.write(''.join(out))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!