|
| 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 | +} |
0 commit comments