Skip to content

Commit 5554c30

Browse files
authored
Create find-common-characters.cpp
1 parent d456ffc commit 5554c30

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

C++/find-common-characters.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Time: O(n * l)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<string> commonChars(vector<string>& A) {
7+
vector<int> count(26, numeric_limits<int>::max());
8+
vector<string> result;
9+
for (const auto& a : A) {
10+
vector<int> tmp_count(count.size());
11+
for (const auto& c : a) {
12+
++tmp_count[c - 'a'];
13+
}
14+
for (int i = 0; i < count.size(); ++i) {
15+
count[i] = min(count[i], tmp_count[i]);
16+
}
17+
}
18+
for (int i = 0; i < count.size(); ++i) {
19+
for (auto j = 0; j < count[i]; ++j) {
20+
result.emplace_back(1, i + 'a');
21+
}
22+
}
23+
return result;
24+
}
25+
};

0 commit comments

Comments
 (0)