Gems

QUESTION

Mia 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 if it occurs at least once in each of the rocks.\n\nGiven the list of N rocks with their compositions, display the number of gems that exist in those rocks.\n\nInput Format\n\nThe first line consists of an integer, N, the number of rocks. \nEach of the next N lines contains a rock’s composition. Each composition consists of lower-case letters of English alphabet.\n\nConstraints \n 1<=N<=100\nEach composition consists of only lower-case Latin letters (‘a’-‘z’). \n 1<=length of each composition<=100 \n\nOutput Format\n\nPrint the number of gems that are common in these rocks. If there are none, print 0.

ANSWER

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,ans=0,ar[109][26]={},j,flag;
    cin >> n;
    string s;
    for(i=0; i<n; i++)
    {
    cin >> s;
    for(j=0; j<s.size(); j++)
        ar[i][s[j]-'a']++;
    }
    for(i=0; i<26; i++)
    {
    flag=0;
    for(j=0; j<n; j++)
        if(ar[j][i]==0)flag=1;
    if(flag==0)ans++;
    }
    cout << ans << endl;
    return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.