1002. 查找常用字符

给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。

你可以按任意顺序返回答案。

示例 1:

输入:["bella","label","roller"]
输出:["e","l","l"]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-common-characters
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

计数

ans.emplace_back(1, 'a' + i);表示将'a'+i 重复1次后 插入,如果是2的话,就是重复2次后插入

class Solution {
public:
    vector<string> commonChars(vector<string>& A) {
        vector <int> vec(26, INT_MAX);
        vector <int> tmp(26);

        for (auto s : A) {
            fill(tmp.begin(), tmp.end(), 0);
            for (auto ch : s) {
                tmp[ch - 'a']++;
            }
            for (int i = 0; i < 26; i++) {
                vec[i] = min(vec[i], tmp[i]);
            }
        }

        vector <string> ans;
        for (int i = 0; i < 26; i++) {
            for (int j = 0; j < vec[i]; j++) {
                ans.emplace_back(1, 'a' + i);
            }
        }

        return ans;

    }
};
原文地址:https://www.cnblogs.com/xgbt/p/13818165.html