Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 56dc31b

Browse files
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)

AltStyle によって変換されたページ (->オリジナル) /