-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gemstones.cpp
70 lines (51 loc) · 1.26 KB
/
Gemstones.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Title : Gemstones
* Author : Tridib Samanta
* Link : https://www.hackerrank.com/challenges/gem-stones/problem
**/
#include <bits/stdc++.h>
using namespace std;
int gemstones(vector<string> arr) {
int n = arr.size();
if (n == 0)
return 0;
bool freq[n][26];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 26; ++j) {
freq[i][j] = false;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; arr[i][j]; ++j) {
freq[i][arr[i][j] - 'a'] = true;
}
}
int gemStones = 0;
for (int i = 0; i < 26; ++i) {
bool isGem = true;
for (int j = 0; j < n; ++j) {
if (!freq[j][i])
isGem = false;
}
if (isGem)
++gemStones;
}
return gemStones;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
vector<string> arr(n);
for (int i = 0; i < n; i++) {
string arr_item;
getline(cin, arr_item);
arr[i] = arr_item;
}
int result = gemstones(arr);
fout << result << "\n";
fout.close();
return 0;
}