-
Notifications
You must be signed in to change notification settings - Fork 0
/
sameCharacters.cpp
53 lines (41 loc) · 1.47 KB
/
sameCharacters.cpp
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
/*//^ 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 "util.h"
#include <vector>
//& Looking at thier oputput it seems that they have used a ordered map or something like that, we can try that too
void findCommonCharacters(std::string words[], int size) //^ using the new find function or the inbuilt string function can be used to reduce the number of loops
{
std::vector<char> commonChars;
for (int i = 0; i < words[0].length(); i++) //^ loops through all the strings
{
bool commonCharacter = false;
for (int j = 1; j < size; j++)
{
for (int k = 0; k < words[j].length(); k++)
{
if (words[j][k] == words[0][i])
{
commonCharacter = true;
break;
}
}
}
if (commonCharacter and util::find(commonChars,words[0][i]) == false) //^ push the current character only if it doesn't already exists in the commonCharacters stack
{
commonChars.push_back(words[0][i]);
}
}
util::display_vector(commonChars);
}
int main()
{
std::string words[] = {"google", "facebook", "youtube"};
findCommonCharacters(words, sizeof(words) / sizeof(words[0]));
return 0;
}