-
Notifications
You must be signed in to change notification settings - Fork 0
/
1002.commonChars.cpp
45 lines (40 loc) · 941 Bytes
/
1002.commonChars.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
// Created by zhaohongyan on 2020/10/14.
//
#include <bits/stdc++.h>
using namespace std;
vector<string> commonChars(vector<string>& A) {
vector<string> res;
map<char,int> cnt;
map<char,int> aa;
for(auto i : A[0]){
cnt[i] ++ ;
aa[i] = 1;
}
for(int i = 1; i < A.size(); i++){
map<char,int> tmp;
for(auto e : A[i]){
tmp[e]++;
}
if(cnt.size()> tmp.size()){
for(auto e : tmp){
if(cnt.count(e.first)){
cnt[e.first] = min(cnt[e.first],e.second);
aa[e.first] ++ ;
}
}
}
}
for(auto i : cnt){
if(aa[i.first] == A.size()){
string tmp = "";
tmp = tmp + i.first;
res.emplace_back(tmp);
}
}
return res;
}
int main(){
vector<string> a = {"bella","label","roller"};
commonChars(a);
}