Suppose we have a string s. We have to encode this by using run-length encoding technique. 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.
So, if the input is like s = "BBBBAAADDCBB", then the output will be "4B3A2D1C2B"
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, s): res = "" tmp = s 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 ob = Solution() print(ob.solve("BBBBAAADDCBB"))