-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFindCommonCharacters_1002.cpp
45 lines (38 loc) · 1.09 KB
/
FindCommonCharacters_1002.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
/*
~ Author : leetcode.com/tridib_2003/
~ Problem : 1002. Find Common Characters
~ Link : https://leetcode.com/problems/find-common-characters/
*/
class Solution {
public:
vector<string> commonChars(vector<string>& A) {
int n = A.size();
int freq[n][26];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 26; ++j) {
freq[i][j] = 0;
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < A[i].length(); ++j) {
++freq[i][A[i][j] - 'a'];
}
}
vector<string> res;
for (int i = 0; i < 26; ++i) {
int minFreq = INT_MAX, j;
for (j = 0; j < n; ++j) {
if (!freq[j][i])
break;
else
minFreq = min(minFreq, freq[j][i]);
}
if (j == n) {
while(minFreq--) {
res.emplace_back(string(1, i + 'a'));
}
}
}
return res;
}
};