Saturday, November 21, 2015

Algorithms -> Strings -> Gemstones

Gemstones


Today I sovled this.The following is the solution for Gemstones in C passed all the test cases.


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

int main() {
char rock[100];
int test,i,a[26]={0},j,count=0;
//printf("\nenter number of test cases:");
scanf("%d",&test);
for(i=0;i<test;i++)
{
//printf("\nenter the string:");
scanf("%s",rock);
for(j=0;rock[j]!='\0';j++)
{
if(a[rock[j]-97]==i)
a[rock[j]-97]++;
}
}
for(i=0;i<26;i++)
{
 if(a[i]==test)
            count++;
}
//printf("the common element is:%d",count);
    printf("%d",count);
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */   
    return 0;
}




No comments:

Post a Comment