HackerRank 'Gemstones' Solution

by Srikant Padala on June 15, 2016, 7:57 am


Explanation


If a letter is not there in any of the stones, then it is not a Gem stone


Gemstones Problem Statement

Video

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int getGemCount(vector<string> &rocks) {
    int count = 0;
    vector<bool> gem(26);
    for(int l = 'a'; l <= 'z'; l++) {
        bool flag = true;
        for(int i=0; i < rocks.size(); i++) {
            if(rocks[i].find(l) == string::npos) {
                gem[i] = false;
                flag = false;
                break;
            }
        }
        if(flag)
            count++;
    }
    return count;
}

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    int n;
    cin >> n;
    vector<string> rocks(n);
    for(int i=0; i < n; i++) {
        cin >> rocks[i];
    }
    
    cout << getGemCount(rocks);
    return 0;
}

Coming Soon.