Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given a string, print the run-length encoded output.
Input Size : N <= 100000
Sample Testcase :
INPUT
aaab
OUTPUT
a3b1

the code I tried executed for above sample case but my problem is private cases didn't pass
I am sorry if this is a naive doubt I am a beginner for coding
thank you in advance

What I have tried:

from collections import Counter
text=input()
if(text==None):
    print(" ")
if(text.isalpha()):
    
        dict1=dict(Counter(text))
        for i,j in dict1.items():
            print(i,j,end="",sep="")
Posted
Updated 10-Oct-19 7:14am
Comments
Richard MacCutchan 10-Oct-19 10:31am    
We cannot guess what tests failed or how they failed.
_gvsss 10-Oct-19 10:50am    
yeah I know that part, my intention was am I missing anything if any please let me know
Richard MacCutchan 10-Oct-19 10:55am    
Yes, you have not explained what the problem is.
k5054 10-Oct-19 11:00am    
What happens when you try the input "aaabbaa"? The output should be a3b2a2
_gvsss 10-Oct-19 11:28am    
no according to my code it will be
aaabbaa
a5b2

1 solution

The Python implementation of the C programmer :-O
Python
s = 'aaabbaa'
c = s[0]
n = 1
o = c
for k in range(1, len(s)):
  o = s[k]
  if o == c:
    n = n + 1
  else:
    print(c,n, sep='', end='')
    n = 1 
    c = o
print(o,n, sep='')
 
Share this answer
 
Comments
_gvsss 11-Oct-19 2:43am    
thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900