Instagram
youtube
Facebook
Twitter

Digit Frequency HackerRank Solution

Given a string, s, consisting of alphabets and digits, find the frequency of each digit in the given string.

Input Format

The first line contains a string, num which is the given number.

Constraints

1 <= len(num) <= 1000

All the elements of num are made of english alphabets and digits.

Output Format

Print ten space-separated integers in a single line denoting the frequency of each digit from 0 to 9.

Sample Input 0

a11472o5t6

Sample Output 0

 

0 2 1 0 1 1 1 1 0 0 

 

Explanation 0

In the given string:

  • 1 occurs two times.

  • 2,4,5,6 and 7 occur one time each.

  • The remaining digits 0, 3, 8 and 9 don’t occur at all.

 

 Sample Input 1

lw4n88j12n1

Sample Output 1

0 2 1 0 1 0 0 0 2 0 

 Sample Input 2

1v88886l256338ar0ekk

Sample Output 2

1 1 1 2 0 1 2 0 5 0 

 

Solution:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  
    char s[1000];
    int freq[10] = {0}; 

    scanf("%[^\n]", s); 
    for (int i = 0; i < strlen(s); i++) {
        if (s[i] >= '0' && s[i] <= '9') {
            freq[s[i] - '0']++;
        }
    }

    for (int i = 0; i < 10; i++) {
        printf("%d ", freq[i]);
    }  
    return 0;
}

Steps used in solving the problem - 

  • First we have imported required header files.

  • Then, we created the main function and initialized an character array of size 1000 and integer array of size 10.

  • Then, we used scanf function to read string from input and used an for loop to iterate through each character of the string.

  • Then, we used an if condition to check if the character is a digit. 

  • At last, we printed the frequency of each digit.