-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1002_Find_Common_Characters.cpp
More file actions
37 lines (36 loc) · 1.06 KB
/
Copy path1002_Find_Common_Characters.cpp
File metadata and controls
37 lines (36 loc) · 1.06 KB
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
/*
1002. Find Common Characters
Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.
Example 1:
Input: words = ["bella","label","roller"]
Output: ["e","l","l"]
Example 2:
Input: words = ["cool","lock","cook"]
Output: ["c","o"]
Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 100
words[i] consists of lowercase English letters.
*/
class Solution {
public:
vector<string> commonChars(vector<string>& words) {
vector<int> min_freq(26, INT_MAX);
for (auto& word : words) {
vector<int> freq(26, 0);
for (char c : word) {
freq[c - 'a']++;
}
for (int i = 0; i < 26; ++i) {
min_freq[i] = min(min_freq[i], freq[i]);
}
}
vector<string> result;
for (int i = 0; i < 26; ++i) {
while (min_freq[i]-- > 0) {
result.push_back(string(1, i + 'a'));
}
}
return result;
}
};