Frequency Count

 Coding Problem Keys 

 Frequency Count 

 Problem Statement 

Given a string, find the frequency 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 

input1: the input string

 Output Specification 

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

 Example Input/Output 1 

 Input 

input1: babdc

 Output 

output: a1b2c1d1

 Explanation 

In the input string, 'a' appears once, 'b' appears twice, 'c' and 'd' appears once. Therefore, in alphabetical order, the output should be: a1b2c1d1.

 Example Input/Output 2 

 Input 

input1: phqgh

 Output 

g1h2p1q1

 Explanation 

In the input string, 'g' appears once, 'h' appears twice, 'p' and 'q' appears once. Therefore, in alphabetical order, the output should be: g1h2p1q1.

 Solution 

 Programming Language: Python 3 Language 

def frequency(a):
    a=list(a)
    p=sorted(a)
    n1=[];
    for i in p:
        if i not in n1:
            n1.append(str(i))
            n1.append(str(p.count(i)))
    for i in n1:
        s=''.join(n1)
    return s
a=input()
print(frequency(a))

# Published By PKJCODERS

 Programming Language: C++ Language 

#include <stdio.h>
#include <string.h>
int main(){
    char s[1000];  
    int  i,j,count=0,n,temp;
    scanf("%s",s);
    for(j=0;s[j];j++);
        n=j;
    for (i=0;i<n-1;i++){
	for (j=i+1;j<n;j++){
	    if(s[i]>s[j]){
	        temp=s[i];
		s[i]=s[j];
		s[j]=temp;
	    }
	}
    }
    for(i=0;i<n;i++){
        count=1;
        if(s[i]){
            for(j=i+1;j<n;j++){
                if(s[i]==s[j]){
                    count++;
                    s[j]='\0';
                }
            }printf("%c%d",s[i],count);    
        }
    }
}

// Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Flowchart Logic 047

Max Winning Streak Game Points

Interface Implementation - Pair

Minimum Sum of Non-Negative Elements

Desktop Products

Replace Diagonals based on Diagonals Sum