Hackerrank solution - Digit Frequency:
Welcome back Guys!!
In this post we will solve Digit fequency Hackerrank problem.
It is a medium level problem using concept of Character Array and ASCII value ...
All the elements of num are made of english alphabets and digits.
You can make a frequency array and then print it but its take more time so i have avoided it.
#include<stdio.h>
#include<string.h>
int main()
{ char s[1001],i;
int f,j;
scanf("%s",s);
for(i=48;i<58;i++)
{ f=0;
for(j=0;j<strlen(s);j++)
{
if(s[j]==i)
{
f++;
}
}
printf("%d ",f);
}
return 0;
}
Problem statement for Digit Frequency Hackerrank problem is as follows:
Given a string, , consisting of alphabets and digits, find the frequency of each digit in the given string.
Input Format
The first line contains a string, which is the given number.
Constraints
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 to .
Sample Input 0
a11472o5t6
Sample Output 0
0 2 1 0 1 1 1 1 0 0
Explanation 0
In the given string:
- occurs two times.
- and occur one time each.
- The remaining digits and 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 Code for Digit Frequency Hackkerrank problem is as follows :
In this problem I have used only scanf ("%s",s) as theres no space in the given string. Now we know the ASCII value of 0 is 48 and that of 9 is 57 . So in this problem treating them as a charater i will search them using ASCII value and get there frequency.You can make a frequency array and then print it but its take more time so i have avoided it.
#include<stdio.h>
#include<string.h>
int main()
{ char s[1001],i;
int f,j;
scanf("%s",s);
for(i=48;i<58;i++)
{ f=0;
for(j=0;j<strlen(s);j++)
{
if(s[j]==i)
{
f++;
}
}
printf("%d ",f);
}
return 0;
}
Please read the code and analysis it .
Feel free to share your thoughts and doubt in the comment section below.
See you next time.
10 Comments
why i is 48?
ReplyDeleteascii value of zero is 48
DeleteThis comment has been removed by the author.
ReplyDeletewhen i compare d it with "s" 0 to 9,the sum didn.t count why?
ReplyDeleteThis comment has been removed by the author.
ReplyDeletelike in abcf789 how does it print '0' of abcd
DeleteIt is showing error
ReplyDeleteDigit Frequency in C – Hacker Rank Solution
ReplyDeletehttps://www.codeworld19.com/digit-frequency-in-c-hacker-rank-solution/
I wrote this code but why it ain't working for test case 0, can somebody pls explain, 2 testcases passed but one failed, it failed for the one string which had more than 20 elements in it.
ReplyDelete#include
#define M 50
int main() {
char s[M],s1[M];
scanf("%[^\n]",s);
int i,t,k=0,rem,seen[10]={0};
int len=strlen(s);
for(i=0;i=1){
seen[rem]++;
}
else{
seen[rem]=1;
}
n=n/10;
}
for(i=0;i<10;i++){
printf("%d ",seen[i]);
}
return 0;
}
Answer in Python
ReplyDeletes = input()
d = {}
for c in s:
d[c] = d.get(c, 0) + 1
#print (d)
for i in range(10):
print (d.get(str(i),0), end =' ')