I am trying to solve a programming question to convert a string to below form:
input: aaaabbbcc
output: a4b3c2
My code is like below:
def encode(s):
output = []
i = 0
j = 1
while i < len(s) and j < len(s)-1 :
count = 1
output.append(s[j])
while s[i] == s[j] :
count += 1
j+=1
i+=1
output.append(count)
i += 1
j += 1
new_s = "".join(str(x) for x in output)
return new_s
But I am getting the below exception :
Traceback (most recent call last):
File "encode.py", line 30, in
print encode(s)
File "encode.py", line 13, in encode
while s[i] == s[j] :
IndexError: string index out of range
I am not able to understand the bug here. Can someone please help me?