-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP52.java
More file actions
54 lines (50 loc) · 1.53 KB
/
Copy pathP52.java
File metadata and controls
54 lines (50 loc) · 1.53 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
* Project Euler #52 solution
*
* by Jayant Sinha
*
*/
import java.util.HashMap;
public class P52 {
public boolean isSimilar(String first, String second) {
if (first.length() != second.length()) {
return false;
}
HashMap<Character, Integer> hash = new HashMap<Character, Integer>();
for (char c : first.toCharArray()) {
if (hash.get(c) != null) {
int count = hash.get(c);
count++;
hash.put(c, count);
} else {
hash.put(c, 1);
}
}
for (char c : second.toCharArray()) {
if (hash.get(c) != null) {
int count = hash.get(c);
count--;
if (count < 0) {
return false;
}
hash.put(c, count);
} else {
return false;
}
}
for (Integer i : hash.values()) {
if (i.intValue() != 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
P52 p = new P52();
for (int i = 11;;i++){
if(p.isSimilar(String.valueOf(i), String.valueOf(i*2)) && p.isSimilar(String.valueOf(i), String.valueOf(i*3)) && p.isSimilar(String.valueOf(i), String.valueOf(i*4)) && p.isSimilar(String.valueOf(i), String.valueOf(i*5)) && p.isSimilar(String.valueOf(i), String.valueOf(i*6))){
System.exit(i);
}
}
}
}