count_a
来保存每个字符在其所属字符串中出现的最小次数
count
来保存每个字符在每一种字符串中出现的最小次数
最后结果中若有重复的,则count对应字符所保存的次数>0。
class Solution
{
public:
vector<string> commonChars(vector<string> &A)
{
vector<string> res;
vector<int> count(26, INT_MAX);
for (auto a : A)
{
vector<int> count_a(26, 0);
for (auto c : a)
++count_a[c - 'a'];
for (int i = 0; i < 26; i++)
count[i] = min(count_a[i], count[i]);
}
for (int i = 0; i < 26; i++)
{
for (int j = 0; j < count[i]; j++)
{
res.push_back(string(1, i + 'a'));
}
}
return res;
}
};