-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path13_Coupon_Code_Validator.cpp
More file actions
55 lines (46 loc) · 1.23 KB
/
Copy path13_Coupon_Code_Validator.cpp
File metadata and controls
55 lines (46 loc) · 1.23 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
// 3606. Coupon Code Validator
class Solution
{
public:
vector<string> validateCoupons(vector<string> &code,
vector<string> &businessLine,
vector<bool> &isActive)
{
// Business line priority
unordered_map<string, int> priority = {
{"electronics", 0},
{"grocery", 1},
{"pharmacy", 2},
{"restaurant", 3}};
vector<pair<int, string>> valid;
for (int i = 0; i < code.size(); i++)
{
if (isActive[i] &&
priority.count(businessLine[i]) &&
isValidCode(code[i]))
{
valid.push_back({priority[businessLine[i]], code[i]});
}
}
// Sort by priority, then by code
sort(valid.begin(), valid.end());
vector<string> result;
for (auto &p : valid)
{
result.push_back(p.second);
}
return result;
}
private:
bool isValidCode(const string &s)
{
if (s.empty())
return false;
for (char c : s)
{
if (!isalnum(c) && c != '_')
return false;
}
return true;
}
};