Program to encode a string in normal form to its run-length form in Python

Run-length encoding is a simple compression technique that replaces consecutive identical characters with a count followed by the character. For example, "AAABBC" becomes "3A2B1C".

So, if the input is like s = "BBBBAAADDCBB", then the output will be "4B3A2D1C2B".

Algorithm

To solve this, we will follow these steps ?

  • Initialize an empty result string
  • Set tmp to the first character and count to 1
  • Iterate through the string starting from index 1
  • If current character differs from tmp, append count and tmp to result
  • Otherwise, increment the count
  • Finally, append the last count and character

Example

Let us see the following implementation to get better understanding ?

class Solution:
    def solve(self, s):
        if not s:
            return ""
        
        res = ""
        tmp = s[0]
        count = 1
        
        for i in range(1, len(s)):
            if s[i] != tmp:
                res += str(count) + tmp
                tmp = s[i]
                count = 1
            else:
                count += 1
        
        return res + str(count) + tmp

# Test the solution
ob = Solution()
print(ob.solve("BBBBAAADDCBB"))
4B3A2D1C2B

Alternative Approach Using itertools.groupby

Python's itertools module provides a more concise solution ?

from itertools import groupby

def run_length_encode(s):
    return ''.join(str(len(list(group))) + key for key, group in groupby(s))

# Test the function
print(run_length_encode("BBBBAAADDCBB"))
print(run_length_encode("AABBCC"))
4B3A2D1C2B
2A2B2C

How It Works

The algorithm works by:

  • Tracking the current character and its count
  • When a different character is encountered, it appends the count and character to the result
  • The final count and character are added after the loop ends

Conclusion

Run-length encoding efficiently compresses strings with repeated characters. The manual approach gives you control over the process, while itertools.groupby provides a more Pythonic solution.

Updated on: 2026-03-25T10:30:19+05:30

559 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements