forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain3.cpp
More file actions
52 lines (40 loc) · 1.32 KB
/
main3.cpp
File metadata and controls
52 lines (40 loc) · 1.32 KB
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
46
47
48
49
50
51
52
/// Source : https://leetcode.com/problems/find-common-characters/
/// Author : liuyubobobo
/// Time : 2019-03-03
#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_map>
using namespace std;
/// Using HashMap to get the intersection of every two strings
/// Time Complexity: O(n * ave_len_of_strings)
/// Space Complexity: O(26)
class Solution {
public:
vector<string> commonChars(vector<string>& A) {
vector<unordered_map<char, int>> freqs(A.size());
for(int i = 0; i < A.size(); i ++)
for(char c: A[i])
freqs[i][c] ++;
unordered_map<char, int> res = freqs[0];
for(int i = 1; i < freqs.size(); i ++)
res = intersection(res, freqs[i]);
vector<string> ret;
for(const pair<char, int>& p: res)
for(int i = 0; i < p.second; i ++)
ret.push_back(string(1, p.first));
return ret;
}
private:
unordered_map<char, int> intersection(unordered_map<char, int>& freq1,
unordered_map<char, int>& freq2){
unordered_map<char, int> res;
for(const pair<char, int>& p: freq1)
if(freq2.count(p.first))
res[p.first] = min(p.second, freq2[p.first]);
return res;
}
};
int main() {
return 0;
}