Find Common Characters LeetCode solution in C++

1
class Solution {
2
public:
3
vector<string> commonChars(vector<string> &words) {
4
vector<string> ans;
5
vector<int> v(26, 0);
6
for (int i = 0; i < words.size(); i++) {
7
vector<int> tmp(26, 0);
8
for (auto ch : words[i]) {
9
tmp[ch - 'a']++;
10
}
11
if (i == 0) {
12
for (int i = 0; i < 26; i++) v[i] = tmp[i];
13
} else
14
for (int i = 0; i < 26; i++) v[i] = min(v[i], tmp[i]);
15
}
16
for (int i = 0; i < 26; i++) {
17
while (v[i]--) {
18
string s;
19
char c = 'a' + i;
20
s.push_back(c);
21
ans.push_back(s);
22
}
23
}
24
return ans;
25
}
26
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0