|
| 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