-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonChars-LCQ.cpp
More file actions
55 lines (44 loc) · 1.21 KB
/
CommonChars-LCQ.cpp
File metadata and controls
55 lines (44 loc) · 1.21 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
53
54
55
/*
Find Common Characters
Given a string array words, return an array of all characters that show up in all strings within the words (including duplicates). You may return the answer in any order.
Example 1:
Input: words = ["bella","label","roller"]
Output: ["e","l","l"]
Example 2:
Input: words = ["cool","lock","cook"]
Output: ["c","o"]
Constraints:
1 <= words.length <= 100
1 <= words[i].length <= 100
words[i] consists of lowercase English letters.
*/
class Solution {
public:
vector<string> commonChars(vector<string>& words) {
vector<int>v1(26,0);
vector<int>v2(26,0);
for(int i=0;i<words[0].size();i++){
v1[words[0][i]-'a']++;
}
for(int i=1;i<words.size();i++){
for(int j=0;j<words[i].size();j++){
v2[words[i][j]-'a']++;
}
for(int k=0;k<26;k++){
v1[k]=min(v1[k],v2[k]);
v2[k]=0;
}
}
vector<string>res;
for(int i=0;i<26;i++){
if(v1[i]>0){
while(v1[i]--){
string s = "";
s+=i+'a';
res.push_back(s);
}
}
}
return res;
}
};