1002. Find Common Characters 寻找公共字符

1002. Find Common Characters 寻找公共字符

题目描述

// Author: Tecker
// time: O(N)
// space: O(1)

vector<string> commonChars(vector<string>& A) {
    vector<string> res;
    vector<int> cnt(26, INT_MAX);
    int tmp[26]={0};
    for(auto s : A) {
        for(auto c : s) ++tmp[c-'a'];
        for(int j=0;j<26;++j) cnt[j] = min(cnt[j], tmp[j]);
        fill(tmp, tmp+26, 0);
    }

    for(int i=0;i<26;++i)
        for(int j=0;j<cnt[i];++j) res.push_back(string(1, i+'a'));
    return res;
}

#字符串