版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35747066/article/details/89419100
1002. Find Common Characters
Easy
18023FavoriteShare
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"]
vector<string> commonChars(vector<string>& A){
vector<string> Result;
vector<unordered_map<char,int>> Vec;
for(int i = 0;i < A.size();i ++){
unordered_map<char,int> tmp;
for(int j = 0;j < A[i].size();j ++){
tmp[A[i][j]]++;
}
Vec.push_back(tmp);
}
unordered_map<char,int> tmp = Vec[0];
for(unordered_map<char,int>::iterator it = tmp.begin();it != tmp.end();it ++){
int Num = it->second;
for(int i = 0 ;i < Vec.size();i ++){
if(Vec[i][it->first] == 0){
Num = 0;
break;
}
else{
Num = min(Num,Vec[i][it->first]);
}
}
for(int i = 0;i < Num;i ++){
string tmp = "";
tmp = tmp + it->first;
Result.push_back(tmp);
}
}
return Result;
}