【题目】
给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。
来源:leetcode
链接:https://leetcode-cn.com/problems/find-common-characters/
【示例1】
输入:[“bella”,“label”,“roller”]
输出:[“e”,“l”,“l”]
【示例2】
输入:[“cool”,“lock”,“cook”]
输出:[“c”,“o”]
【提示】
1 <= A.length <= 100
1 <= A[i].length <= 100
A[i][j] 是小写字母
【代码】
class Solution {
public:
int hashrs[100][26]={0};
string str[26]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",
"p","q","r","s","t","u","v","w","x","y","z"};
vector<string> commonChars(vector<string>& A) {
vector<string> rs;
int cnt=0;
for(auto x:A){
for(auto y:x)
hashrs[cnt][y-'a']++;
cnt++;
}
for(int i=0;i<26;i++){
int minnum=100;
for(int j=0;j<A.size();j++)
minnum=min(minnum,hashrs[j][i]);
for(int j=0;j<minnum;j++)
rs.push_back(str[i]);
}
return rs;
}
};