-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathsolution.java
More file actions
51 lines (42 loc) · 1.41 KB
/
Copy pathsolution.java
File metadata and controls
51 lines (42 loc) · 1.41 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
class Solution {
public List<String> validateCoupons(String[] code,
String[] businessLine,
boolean[] isActive) {
Map<String, Integer> priority = new HashMap<>();
priority.put("electronics", 0);
priority.put("grocery", 1);
priority.put("pharmacy", 2);
priority.put("restaurant", 3);
List<String[]> valid = new ArrayList<>();
for (int i = 0; i < code.length; i++) {
if (!isActive[i])
continue;
if (!priority.containsKey(businessLine[i]))
continue;
if (code[i].isEmpty())
continue;
boolean ok = true;
for (char c : code[i].toCharArray()) {
if (!Character.isLetterOrDigit(c) && c != '_') {
ok = false;
break;
}
}
if (!ok)
continue;
valid.add(new String[] { businessLine[i], code[i] });
}
valid.sort((a, b) -> {
int p1 = priority.get(a[0]);
int p2 = priority.get(b[0]);
if (p1 == p2)
return a[1].compareTo(b[1]);
return p1 - p2;
});
List<String> result = new ArrayList<>();
for (String[] v : valid) {
result.add(v[1]);
}
return result;
}
}