
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Decode Run-Length 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"
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
- if i is alphabet, then
- return output
Let us see the following implementation to get better understanding −
Example
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"))
Input
"4B3A2D1C2B"
Output
BBBBAAADDCBB
Advertisements