Thursday, June 8, 2017

String - Characters count

A string S containing N unique characters is passed as input to the program. The program must print the character and it's occurrence count in N lines of output. The characters with their count in the output are in the same order of occurrence as in the string S. Note: All the characters will be alphabets and in lower case. 

Input Format: 

The first line contains the value of S 

Output Format: 

N lines containing the character and it's count. 

Example Input/Output 1:

Input: 
programming 

Output: 
p1 
r2 
o1 
g2 
a1 
m2 
i1 
n1 

Example Input/Output 2: 

Input: 
engineering 

Output: 
e3 
n3 
g2 
i2 
r1

Solution: 

s = input()
c = [0]*26
a = []*len(s)
for i in range(len(s)):
    c[ord(s[i])-ord('a')] += 1
    if c[ord(s[i])-ord('a')] == 1:
        a.append(s[i])
for i in range(len(a)):
    print(a[i]+str(c[ord(a[i])-ord('a')]))


Share: