Accenture Coding Question | Frequency Count | CodeWindow

Frequency Count

Given a string, find the frequencies of each of the characters in it.
The input string contains only lowercase letters. The output string should contain a letter followed by its frequency, in the alphabetical order (from a to z).

Input specification:
Input 1: The input string.

Output Specification:
Return a string representing the frequency counts of characters in the input string.

Example 1:
Input 1: babdc

Output: a1b2c1d1
Explanation:
In the input string, ‘a’ appears once, ‘b’ appears twice, ‘c’ and ‘d’ appear once.

Solution in Python 3:

s=input()
s=list(s)
no_rep = sorted(list(set(s)))
for i in no_rep:
    print(i,end="")
    print(s.count(i),end="")
    
#join Telegram @codewindow

Alternative Method

x1=input()
x=sorted(set(x1))
for i in x:
    print(i,end=str(x1.count(i)))
    
#join Telegram @codewindow

Output:

babdc
a1b2c1d1
Recent Posts

Leave a Comment

Your email address will not be published. Required fields are marked *