HACKER RANK Algorithm Strings Challenges - Gemstones Solution.'C'
Algorithm Strings Challenges - Gemstones Solution
Problem Statement
John has discovered various rocks. Each rock is composed of various elements, and each element is represented by a lower-case Latin letter from 'a' to 'z'. An element can be present multiple times in a rock. An element is called a gem-element if it occurs at least once in each of the rocks.
Given the list of N rocks with their compositions, display the number of gem-elements that exist in those rocks.
Input Format
The first line consists of an integer, N , the number of rocks.
Each of the next N lines contains a rock's composition. Each composition consists of lower-case letters of English alphabet.
Constraints
1≤N≤100
Each composition consists of only lower-case Latin letters ('a'-'z').
1≤ length of each composition ≤100
Output Format
Print the number of gem-elements that are common in these rocks. If there are none, print 0.
Sample Input
3
abcdde
baccd
eeabg
Sample Output
2
Explanation
Only "a" and "b" are the two kinds of gem-elements, since these are the only characters that occur in every rock's composition.
Problem Statement
John has discovered various rocks. Each rock is composed of various elements, and each element is represented by a lower-case Latin letter from 'a' to 'z'. An element can be present multiple times in a rock. An element is called a gem-element if it occurs at least once in each of the rocks.
Given the list of N rocks with their compositions, display the number of gem-elements that exist in those rocks.
Input Format
The first line consists of an integer, N , the number of rocks.
Each of the nextN lines contains a rock's composition. Each composition consists of lower-case letters of English alphabet.
Each of the next
Constraints
1≤N≤100
Each composition consists of only lower-case Latin letters ('a'-'z').
1≤ length of each composition ≤100
Each composition consists of only lower-case Latin letters ('a'-'z').
Output Format
Print the number of gem-elements that are common in these rocks. If there are none, print 0.
Sample Input
3
abcdde
baccd
eeabg
Sample Output
2
Explanation
Only "a" and "b" are the two kinds of gem-elements, since these are the only characters that occur in every rock's composition.
Solution:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int i,j,n,k,cnt=0,c=0;
scanf("%d",&n);
char str[n][100];
int h1[n][26];
for(i=0;i<n;i++)
for(j=0;j<26;j++)
h1[i][j]=0;
for(i=0;i<n;i++)
{
fflush(stdin);
scanf("%s",str[i]);
for(j=0;j<strlen(str[i]);j++)
{
h1[i][(str[i][j])-97]=1;
}
}
i=0;
for(j=0;j<26;j++)
{
cnt=0;
for(k=1;k<n;k++)
{
if(h1[i][j]!=0)
{
if((h1[i][j]==h1[k][j]))
cnt++;
}
}
if(cnt==n-1)
{
c++;
}
}
printf("%d",c);
return 0;
}
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int i,j,n,k,cnt=0,c=0; scanf("%d",&n); char str[n][100]; int h1[n][26]; for(i=0;i<n;i++) for(j=0;j<26;j++) h1[i][j]=0; for(i=0;i<n;i++) { fflush(stdin); scanf("%s",str[i]); for(j=0;j<strlen(str[i]);j++) { h1[i][(str[i][j])-97]=1; } } i=0; for(j=0;j<26;j++) { cnt=0; for(k=1;k<n;k++) { if(h1[i][j]!=0) { if((h1[i][j]==h1[k][j])) cnt++; } } if(cnt==n-1) { c++; } } printf("%d",c); return 0; }
Thanks for Visiting, Hope this helps you....
Copyright © 2015 HackerRank.
All Rights Reserved
All Rights Reserved
Comments
Post a Comment