-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindCommonCharacters.cpp
More file actions
33 lines (26 loc) · 866 Bytes
/
FindCommonCharacters.cpp
File metadata and controls
33 lines (26 loc) · 866 Bytes
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
class Solution {
public:
vector<string> commonChars(vector<string>& A) {
unordered_map<char, int> Map;
for(int i=0; i<A[0].length(); i++){
Map[A[0][i]]++;
}
for(int i=1; i<A.size(); i++){
unordered_map<char, int> Temp;
for(int j=0; j<A[0].length(); j++){
Temp[A[i][j]]++;
}
for(auto itr = Map.begin(); itr != Map.end(); itr++){
itr->second = min(itr->second, Temp[itr->first]);
}
}
vector<string> Sol;
for(auto itr = Map.begin(); itr != Map.end(); itr++){
for(int i = 0; i<itr->second; i++){
string S(1, itr->first);
Sol.push_back(S);
}
}
return Sol;
}
};