# 1002. Find Common Characters

Given an array `A` of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates).  For example, if a character occurs 3 times in all strings but not 4 times, you need to include that character three times in the final answer.

You may return the answer in any order.

Example 1:

``````Input: ["bella","label","roller"]
Output: ["e","l","l"]
``````

Example 2:

``````Input: ["cool","lock","cook"]
Output: ["c","o"]
``````

Note:

1. `1 <= A.length <= 100`
2. `1 <= A[i].length <= 100`
3. `A[i][j]` is a lowercase letter

``````class Solution {
public:
vector<string> commonChars(vector<string>& A) {
vector<string> res;
vector<int> cnt(26, INT_MAX);
for (string word : A) {
vector<int> t(26);
for (char c : word) ++t[c - 'a'];
for (int i = 0; i < 26; ++i) {
cnt[i] = min(cnt[i], t[i]);
}
}
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < cnt[i]; ++j) {
res.push_back(string(1, 'a' + i));
}
}
return res;
}
};
``````

Github 同步地址:

https://github.com/grandyang/leetcode/issues/1002

Intersection of Two Arrays II

Intersection of Two Arrays

https://leetcode.com/problems/find-common-characters/

https://leetcode.com/problems/find-common-characters/discuss/247573/C%2B%2B-O(n)-or-O(1)-two-vectors

LeetCode All in One 题目讲解汇总(持续更新中…)

 微信打赏 Venmo 打赏
（欢迎加入博主的知识星球，博主将及时答疑解惑，并分享刷题经验与总结，试运营期间前五十位可享受半价优惠～）

×

Help us with donation