forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain2.cpp
More file actions
48 lines (36 loc) · 1.16 KB
/
main2.cpp
File metadata and controls
48 lines (36 loc) · 1.16 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
/// Source : https://leetcode.com/problems/find-common-characters/
/// Author : liuyubobobo
/// Time : 2019-03-03
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
/// Sorting and using C++ STL set_intersection :-)
/// Time Complexity: O(n * ave_len_of_strings)
/// Space Complexity: O(ave_len_of_strings)
class Solution {
public:
vector<string> commonChars(vector<string>& A) {
for(string& s: A) sort(s.begin(), s.end());
string res = A[0];
for(int i = 1; i < A.size(); i ++){
string cur(max(res.size(), A[i].size()), ' ');
string::iterator iter = set_intersection(res.begin(), res.end(), A[i].begin(), A[i].end(), cur.begin());
while(cur.size() && cur.back() == ' ') cur.pop_back();
res = cur;
}
vector<string> ret;
for(char c: res)
ret.push_back(string(1, c));
return ret;
}
};
void print_vec(const vector<string>& vec){
for(const string& s: vec) cout << s << " ";
cout << endl;
}
int main() {
vector<string> A1 = {"bella","label","roller"};
print_vec(Solution().commonChars(A1));
return 0;
}