Skip to content

Commit bf6e1ab

Browse files
committed
Add C++ solution of problem #3606
1 parent 7c405b5 commit bf6e1ab

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

3606/solution.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution {
2+
public:
3+
vector<string> validateCoupons(vector<string>& code, vector<string>& businessLine, vector<bool>& isActive) {
4+
vector<vector<string>> m(4); // "electronics", "grocery", "pharmacy", "restaurant"
5+
int n = code.size();
6+
for(int i = 0; i < n; i++) {
7+
if(!isActive[i])
8+
continue;
9+
10+
string c = code[i];
11+
if(!all_valid_chars(c))
12+
continue;
13+
14+
string b = businessLine[i];
15+
if(b == "electronics")
16+
m[0].push_back(c);
17+
else if(b == "grocery")
18+
m[1].push_back(c);
19+
else if(b == "pharmacy")
20+
m[2].push_back(c);
21+
else if(b == "restaurant")
22+
m[3].push_back(c);
23+
}
24+
25+
for(auto &v : m)
26+
ranges::sort(v);
27+
28+
vector<string> ans;
29+
for(auto v : m)
30+
ans.insert(ans.end(), v.begin(), v.end());
31+
return ans;
32+
}
33+
34+
private:
35+
bool all_valid_chars(const string &s) {
36+
if(s.empty())
37+
return false;
38+
39+
for(char c : s) {
40+
if(!isalnum(c) && c != '_')
41+
return false;
42+
}
43+
return true;
44+
}
45+
};

0 commit comments

Comments
 (0)