Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Program to perform string compression in Python
String compression using run-length encoding replaces consecutive repeated characters with the character followed by its count. For example, 'bbbb' becomes 'b4'. Single characters remain unchanged without a count.
So, if the input is like s = "abbbaaaaaaccdaaab", then the output will be "ab3a6c2da3b".
Algorithm
To solve this, we will follow these steps ?
- Initialize an empty result string and counter set to 1
- Iterate through the string starting from index 1
- If current character matches previous character, increment counter
- Otherwise, append the previous character to result
- If counter > 1, append the count as well
- Reset counter to 1
- Handle the last character after the loop
Implementation
def solve(s):
res = ""
cnt = 1
for i in range(1, len(s)):
if s[i - 1] == s[i]:
cnt += 1
else:
res = res + s[i - 1]
if cnt > 1:
res += str(cnt)
cnt = 1
res = res + s[-1]
if cnt > 1:
res += str(cnt)
return res
s = "abbbaaaaaaccdaaab"
print(solve(s))
The output of the above code is ?
ab3a6c2da3b
How It Works
The algorithm processes the string character by character:
- 'a' appears once ? 'a'
- 'b' appears 3 times ? 'b3'
- 'a' appears 6 times ? 'a6'
- 'c' appears 2 times ? 'c2'
- 'd' appears once ? 'd'
- 'a' appears 3 times ? 'a3'
- 'b' appears once ? 'b'
Alternative Approach Using groupby
Python's itertools.groupby provides a more concise solution ?
from itertools import groupby
def compress_string(s):
result = ""
for char, group in groupby(s):
count = len(list(group))
if count == 1:
result += char
else:
result += char + str(count)
return result
s = "abbbaaaaaaccdaaab"
print(compress_string(s))
ab3a6c2da3b
Conclusion
Run-length encoding efficiently compresses strings with consecutive repeated characters. The manual approach gives full control, while groupby provides a cleaner solution for this common pattern.
Advertisements
