-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathLC_1002_FindCommonCharacters.cpp
50 lines (43 loc) · 1.09 KB
/
LC_1002_FindCommonCharacters.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
/*
https://leetcode.com/problems/find-common-characters/
1002. Find Common Characters
*/
class Solution {
public:
vector<string> commonChars(vector<string>& words) {
vector<string> ans;
// unordered_map<int, int> freq;
// vector<int> freq(26,0);
// int freq[26] = {0};
// int newf[26] = {0};
vector<int> freq(26,0);
vector<int> newf(26,0);
for(char c: words[0])
{
freq[c-'a']++;
}
for(int w=1; w<words.size(); w++)
{
// vector<int> newf(26,0);
for(char c: words[w])
{
newf[c-'a']++;
}
for(int i=0; i<26; i++)
{
// if(newf[i] != freq[i])
freq[i]=min(newf[i], freq[i]);
newf[i]=0;
}
}
for(int i=0; i<26; i++)
{
while(freq[i]--)
{
string s(1, i+'a');
ans.push_back(s);
}
}
return ans;
}//
};