Gemstones

QUESTION

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. \n\nAn element is called a gem-element 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 gem-elements 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 gem-elements that are common in these rocks. If there are none, print 0.

ANSWER

#include<iostream>
#include<string>
using namespace std;
int main()
{
    int T;
    int a[26] = {0};
    bool flag[26] = {false};
    int nCount = 0;     
    cin >> T;
    cin.ignore();
     int curTest = 1;
    while(curTest <= T)
    {
 string in;
 getline(cin,in);

 for(int i = 0; i < in.length();i++)
 {
  int ch = ((int)in[i]) - 97;
  if( ch >= 0 && ch < 26 && flag[ch] == false) 
         {    a[ch]++;
                    flag[ch] = true;
         }
        
 }
        for(int i = 0; i < 26;i++)
         flag[i] = false;
     curTest++;
    }
    
    for(int i = 0 ; i <= 25;i++)
    if(a[i] == T)
  nCount++;
    cout<<nCount;
 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.