Skip to content

Commit 01ac9a2

Browse files
add 3606
1 parent bbc837f commit 01ac9a2

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

  • paginated_contents/algorithms/4th_thousand
  • src

‎paginated_contents/algorithms/4th_thousand/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
| 3978 | [Unique Middle Element](https://leetcode.com/problems/unique-middle-element/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3978.java) | | Easy |
2121
| 3701 | [Compute Alternating Sum](https://leetcode.com/problems/compute-alternating-sum/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3701.java) | | Easy |
2222
| 3662 | [Filter Characters by Frequency](https://leetcode.com/problems/filter-characters-by-frequency/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3662.java) | | Easy |
23+
| 3606 | [Coupon Code Validator](https://leetcode.com/problems/coupon-code-validator/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3606.java) | | Easy |
2324
| 3502 | [Minimum Cost to Reach Every Position](https://leetcode.com/problems/minimum-cost-to-reach-every-position/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3502.java) | | Easy |
2425
| 3491 | [Phone Number Prefix](https://leetcode.com/problems/phone-number-prefix/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3491.java) | | Easy |
2526
| 3477 | [Fruits Into Baskets II](https://leetcode.com/problems/fruits-into-baskets-ii/) | [Java](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/fourththousand/_3477.java) | | Easy |
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.fishercoder.solutions.fourththousand;
2+
3+
import java.util.*;
4+
5+
public class _3606 {
6+
public static class Solution1 {
7+
8+
Set<String> validBiz = new HashSet<>(Arrays.asList("electronics", "grocery", "pharmacy", "restaurant"));
9+
10+
public List<String> validateCoupons(String[] code, String[] businessLine, boolean[] isActive) {
11+
Map<String, List<String>> map = new HashMap<>();
12+
for (int i = 0; i < code.length; i++) {
13+
if (isValid(code[i], businessLine[i], isActive[i])) {
14+
List<String> list = map.getOrDefault(businessLine[i], new ArrayList<>());
15+
list.add(code[i]);
16+
map.put(businessLine[i], list);
17+
}
18+
}
19+
TreeMap<String, List<String>> treeMap = new TreeMap<>();
20+
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
21+
List<String> list = entry.getValue();
22+
Collections.sort(list);
23+
treeMap.put(entry.getKey(), list);
24+
}
25+
List<String> result = new ArrayList<>();
26+
for (Map.Entry<String, List<String>> entry : treeMap.entrySet()) {
27+
result.addAll(entry.getValue());
28+
}
29+
return result;
30+
}
31+
32+
private boolean isValid(String code, String bizLine, boolean isActive) {
33+
if (!isActive || !validBiz.contains(bizLine) || code.equals("")) {
34+
return false;
35+
}
36+
for (char c : code.toCharArray()) {
37+
if (Character.isAlphabetic(c) || Character.isDigit(c) || '_' == c) {
38+
} else {
39+
return false;
40+
}
41+
}
42+
return true;
43+
}
44+
}
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.fishercoder.fourththousand;
2+
3+
import com.fishercoder.solutions.fourththousand._3606;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
11+
public class _3606Test {
12+
private _3606.Solution1 solution1;
13+
14+
@BeforeEach
15+
public void setup() {
16+
solution1 = new _3606.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
String[] code = new String[]{"SAVE20", "", "PHARMA5", "SAVE@20"};
22+
String[] businessLine = new String[]{"restaurant", "grocery", "pharmacy", "restaurant"};
23+
boolean[] isActive = new boolean[]{true, true, true, true};
24+
assertEquals(Arrays.asList("PHARMA5", "SAVE20"), solution1.validateCoupons(code, businessLine, isActive));
25+
}
26+
27+
@Test
28+
public void test2() {
29+
String[] code = new String[]{"GROCERY15", "ELECTRONICS_50", "DISCOUNT10"};
30+
String[] businessLine = new String[]{"grocery", "electronics", "invalid"};
31+
boolean[] isActive = new boolean[]{false, true, true};
32+
assertEquals(Arrays.asList("ELECTRONICS_50"), solution1.validateCoupons(code, businessLine, isActive));
33+
}
34+
}

0 commit comments

Comments
 (0)