File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change 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+ };
You can’t perform that action at this time.
0 commit comments