Hacker Rank Solutions for Gem-Stones

Hacker Rank Solutions for Gem-Stones

John has discovered various rocks. Each rock is composed of various elements, and each element is represented by a lowercase 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 rocks with their compositions, display the number of gem-elements that exist in those rocks.

Input Format 

The first line consists of N, the number of rocks.

Each of the next N lines contain rocks' composition. Each composition consists of lowercase letters of English alphabet.

Output Format 

Print the number of gem-elements that are common in these rocks.

Constraints 

1 ≤ N ≤ 100

Each composition consists of only small latin letters ('a'-'z').

1 ≤ Length of each composition ≤ 100

Sample Input

3
abcdde
baccd
eeabg

Sample Output

2

Explanation 

Only "a", "b" are the two kind of gem-elements, since these are the only characters that occur in each of the rocks' composition.

Solutions

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

   main(){
    int n,i,j, ans=0;
    int count[26]={0};
    char s[100][100];
    char *p;
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%s",s[i]);
    }
    j=1;
    int t, index;
    int l = strlen(s[0]);
        for(i=0;i<l;i++){
            t=0;
                for(j=1;j<n;j++){
                    if(strchr(s[j],s[0][i])!='\0'){
                        t++;
                    }
                }
                        if(t==n-1){
                            index= s[0][i] - 'a';
                            count[index]=1;
                            
                        }
                
            }
    for(i=0;i<26;i++){
        if (count[i]==1)
            ans++;
    }
    printf("%d",ans);
}

No comments:

Post a Comment