Posted on

Description

Submission

class Solution {
    int cnt[100][26];
public:
    vector<string> commonChars(vector<string>& A) {
        int n = A.size();
        vector<int> globalCnt(26, INT_MAX);
        for(auto s: A) {
            vector<int> cnt(26, 0);
            for(auto ch : s) {
                cnt[ch-'a']++;
            }
            for(int i = 0; i < 26; ++i) {
                globalCnt[i] = min(globalCnt[i], cnt[i]);
            }
        }
        vector<string> ret;
        for(int i = 0; i < 26; ++i) {
            for(int j = 0; j < globalCnt[i]; ++j) {
                ret.push_back(string(1, 'a'+i));
            }
        }
        return ret;
    }
};

Leave a Reply

Your email address will not be published. Required fields are marked *