-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash.java
37 lines (29 loc) · 874 Bytes
/
hash.java
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
package day4;
import java.util.*;;
public class hash {
public boolean digitCount(String num) {
HashMap<Integer, Integer> map = new HashMap<>();
for (char c : num.toCharArray()) {
int n = c - '0';
map.put(n, map.getOrDefault(n, 0) + 1);
}
boolean ans = true;
for (int i = 0; i < num.length(); i++) {
if (map.containsKey(i)) {
if (map.get(i) != num.charAt(i) - '0') {
ans = false;
}
} else if (!map.containsKey(i)) {
if (num.charAt(i) - '0' != 0) {
ans = false;
}
}
}
return ans;
}
public static void main(String[] args) {
hash s = new hash();
String num = "1210";
System.out.println(s.digitCount(num));
}
}