-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcommon_characters.cpp
More file actions
75 lines (58 loc) · 1.58 KB
/
common_characters.cpp
File metadata and controls
75 lines (58 loc) · 1.58 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// Created by Mayank Parasar on 2020-05-12.
//
/*
* Given a list of strings, find the list of characters that appear in all strings.
Here's an example and some starter code:
def common_characters(strs):
# Fill this in.
print(common_characters(['google', 'facebook', 'youtube']))
# ['e', 'o']*/
#include <iostream>
#include <string>
#include <vector>
#include <map>
using namespace std;
vector<char>
common_characters(vector<string> word_list) {
vector<char> result;
map<char, int> word_map;
for(int ii=0; ii < word_list.size(); ii++) {
// remove all the repeting characters in this word
map<char, int> word;
for(auto i : word_list[ii])
word[i] = 1;
// now make a string our of this map
string word_;
for(auto i : word) {
word_ += i.first;
}
// cout << word_ << endl;
for(auto k : word_) {
word_map[k]++;
}
}
// whichever letter has frequency same as
// word_list.size() put that into the result
// vector
for(auto k : word_map){
if(k.second == word_list.size())
result.push_back(k.first);
}
return result;
}
int main() {
string str1 = "google";
string str2 = "facebook";
string str3 = "youtube";
vector<string> string_list;
// push strings into the 'string-list'
string_list.push_back(str1);
string_list.push_back(str2);
string_list.push_back(str3);
vector<char> result;
result = common_characters(string_list);
for(auto i : result)
cout << i << " ";
return 0;
}