Program to decode a run-length form of string into normal form in Python

Suppose we have a string s. The s is a run-length encoded string, we have to find the decoded version of it. As we know, run-length encoding is a fast and simple method of encoding strings. The idea is as follows − The repeated successive elements (characters) as a single count and character. For example, if the string is like "BBBBAAADDCBB" would be encoded as "4B3A2D1C2B".

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

Algorithm

To solve this, we will follow these steps ?

  • output := blank string
  • num := blank string
  • for each character i in s, do
    • if i is alphabet, then
      • output := output + i*(num as number)
      • num := blank string
    • otherwise,
      • num := num + i
  • return output

Example

Let us see the following implementation to get better understanding ?

class Solution:
    def solve(self, s):
        output = ""
        num = ""
        for i in s:
            if i.isalpha():
                output += i * int(num)
                num = ""
            else:
                num += i
        return output

ob = Solution()
print(ob.solve("4B3A2D1C2B"))

The output of the above code is ?

BBBBAAADDCBB

Alternative Approach Using Regular Expressions

We can also use regular expressions to extract number-character pairs ?

import re

def decode_run_length(s):
    result = ""
    # Find all patterns of digits followed by a character
    matches = re.findall(r'(\d+)([A-Za-z])', s)
    
    for count, char in matches:
        result += char * int(count)
    
    return result

# Test the function
encoded_string = "4B3A2D1C2B"
decoded = decode_run_length(encoded_string)
print(f"Encoded: {encoded_string}")
print(f"Decoded: {decoded}")
Encoded: 4B3A2D1C2B
Decoded: BBBBAAADDCBB

How It Works

The algorithm processes the encoded string character by character:

  • Digit characters are accumulated in the `num` variable
  • Alphabetic characters trigger the decoding process
  • When an alphabetic character is found, it's repeated `num` times and added to the output
  • The `num` variable is reset for the next count-character pair

Conclusion

Run-length decoding can be efficiently implemented by iterating through the string and separating numeric counts from characters. The regular expression approach provides a more concise solution for pattern-based parsing.

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

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements