-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2094-finding-3-digit-even-numbers.java
More file actions
39 lines (37 loc) · 1.15 KB
/
2094-finding-3-digit-even-numbers.java
File metadata and controls
39 lines (37 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class Solution {
public Map<Integer, Integer> getDigitMap(int n) {
Map<Integer, Integer> digMap = new HashMap<>();
while (n > 0) {
digMap.put(n % 10, digMap.getOrDefault(n % 10, 0) + 1);
n = n / 10;
}
return digMap;
}
public boolean matches(Map<Integer, Integer> digMap) {
for (Map.Entry<Integer, Integer> entry: digMap.entrySet()) {
if (entry.getValue() != 0) {
return false;
}
}
return true;
}
public int[] findEvenNumbers(int[] digits) {
List<Integer> res = new ArrayList<>();
for (int i = 100; i < 1000; i += 2) {
Map<Integer, Integer> digMap = getDigitMap(i);
for (int dig: digits) {
if (digMap.containsKey(dig) && digMap.get(dig) > 0) {
digMap.put(dig, digMap.get(dig) - 1);
}
}
if (matches(digMap)) {
res.add(i);
}
}
int[] ans = new int[res.size()];
for (int i = 0; i < res.size(); i++) {
ans[i] = res.get(i);
}
return ans;
}
}