Skip to content

Commit 56dc31b

Browse files
authored
Create Coupon Code Validator.java
1 parent ebabeb1 commit 56dc31b

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Easy/Coupon Code Validator.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public List<String> validateCoupons(String[] code, String[] businessLine, boolean[] isActive) {
3+
Map<String, List<String>> validBusinessLinesMap = new LinkedHashMap<>();
4+
validBusinessLinesMap.put("electronics", new ArrayList<>());
5+
validBusinessLinesMap.put("grocery", new ArrayList<>());
6+
validBusinessLinesMap.put("pharmacy", new ArrayList<>());
7+
validBusinessLinesMap.put("restaurant", new ArrayList<>());
8+
int n = code.length;
9+
for (int i = 0; i < n; i++) {
10+
if (isActive[i]) {
11+
String currentCode = code[i];
12+
String currentBusinessLine = businessLine[i];
13+
if (validBusinessLinesMap.containsKey(currentBusinessLine) && isValidCode(currentCode)) {
14+
validBusinessLinesMap.get(currentBusinessLine).add(currentCode);
15+
}
16+
}
17+
}
18+
List<String> result = new ArrayList<>();
19+
for (Map.Entry<String, List<String>> entry : validBusinessLinesMap.entrySet()) {
20+
if (!entry.getValue().isEmpty()) {
21+
List<String> codes = new ArrayList<>(entry.getValue());
22+
Collections.sort(codes);
23+
result.addAll(codes);
24+
}
25+
}
26+
return result;
27+
}
28+
29+
private boolean isValidCode(String currentCode) {
30+
if (currentCode == null || currentCode.isBlank()) {
31+
return false;
32+
}
33+
for (char c : currentCode.toCharArray()) {
34+
if (!(Character.isLetter(c) || Character.isDigit(c) || c == '_')) {
35+
return false;
36+
}
37+
}
38+
return true;
39+
}
40+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy