-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhow_many_feelings.cpp
48 lines (34 loc) · 1.32 KB
/
how_many_feelings.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
//You have two arguments: string - a string of random letters(only lowercase) and array - an array of strings(feelings). Your task is to return how many specific feelings are in the array.
//For example:
//string -> 'yliausoenvjw'
//array -> ['anger', 'awe', 'joy', 'love', 'grief']
//output -> '3 feelings.' // 'awe', 'joy', 'love'
//string -> 'griefgriefgrief'
//array -> ['anger', 'awe', 'joy', 'love', 'grief']
//output -> '1 feeling.' // 'grief'
//string -> 'abcdkasdfvkadf'
//array -> ['desire', 'joy', 'shame', 'longing', 'fear']
//output -> '0 feelings.'
//If the feeling can be formed once - plus one to the answer.
//If the feeling can be formed several times from different letters - plus one to the answer.
//Eeach letter in string participates in the formation of all feelings. 'angerw' -> 2 feelings: 'anger' and 'awe'.
#include <string>
std::string countFeelings(std::string s, std::vector<std::string> arr) {
int count = 0;
bool found = false;
std::string tmp;
for (auto word : arr) {
tmp = s;
for (auto letter : word) {
auto pos = tmp.find(letter);
found = pos != std::string::npos;
if (found) {
tmp.erase(pos, 1);
} else {
break;
}
}
if (found) count++;
}
return std::to_string(count) + " feeling" + (count == 1 ? "." : "s.");
}