Python Program for String Compression
String compression is a technique used to reduce the size of a string by replacing repeated characters with the character followed by its count.
It is a common problem that helps in understanding loops, grouping, and basic data compression concepts.
1. Understanding the Problem
Given a string, compress it by replacing consecutive repeated characters with the character and its count.
Input: aaabbc Output: a3b2c1
2. Method 1: Using Loop
string = input("Enter a string: ")
compressed = ""
count = 1
for i in range(len(string)):
if i < len(string) - 1 and string[i] == string[i + 1]:
count += 1
else:
compressed += string[i] + str(count)
count = 1
print(compressed)
This method counts consecutive characters and appends them to the result.
3. Method 2: Using itertools.groupby
from itertools import groupby
string = input()
compressed = ""
for char, group in groupby(string):
compressed += char + str(len(list(group)))
print(compressed)
groupby groups consecutive characters efficiently.
4. Method 3: Using Function
def compress_string(s):
result = ""
count = 1
for i in range(len(s)):
if i < len(s) - 1 and s[i] == s[i + 1]:
count += 1
else:
result += s[i] + str(count)
count = 1
return result
print(compress_string("aaabbc"))
Encapsulating logic inside a function improves reusability.
5. Method 4: Using List and Join
string = input()
result = []
count = 1
for i in range(len(string)):
if i < len(string) - 1 and string[i] == string[i + 1]:
count += 1
else:
result.append(string[i] + str(count))
count = 1
print("".join(result))
Using a list improves performance compared to string concatenation.
6. Method 5: Return Original if Not Smaller
def compress(s):
result = ""
count = 1
for i in range(len(s)):
if i < len(s) - 1 and s[i] == s[i + 1]:
count += 1
else:
result += s[i] + str(count)
count = 1
return result if len(result) < len(s) else s
print(compress("abc"))
This method ensures compression only if it reduces string size.
7. Algorithm
1. Take input string.
2. Initialize count = 1.
3. Traverse string.
4. Count consecutive characters.
5. Append character and count.
6. Print compressed string.
8. Common Mistakes
1. Not resetting count properly.
2. Incorrect indexing.
3. Missing last character group.
4. Inefficient string concatenation.
9. Applications
1. Data compression basics.
2. Encoding techniques.
3. Reducing storage size.
4. Algorithm design practice.
Conclusion
String compression is a classic problem that introduces you to optimization and grouping techniques in Python.
Using itertools.groupby is the most elegant method, while loop-based solutions help build strong fundamentals.
Codecrown