-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAnagramCounting.java
More file actions
52 lines (35 loc) · 1009 Bytes
/
AnagramCounting.java
File metadata and controls
52 lines (35 loc) · 1009 Bytes
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
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class AnagramCounting {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
String in = sc.nextLine();
int[] cnt = new int[26*2];
for (int i = 0 ; i < in.length(); i++) {
char curr = in.charAt(i);
if (curr >='a' && curr <='z') {
cnt[curr-'a']++;
}
if (curr >='A' && curr <='Z') {
cnt[curr-'A'+26]++;
}
}
BigInteger b = new BigInteger("1");
int stack = 1;
for (int i =0 ; i < cnt.length; i++) {
if (cnt[i] != 0) {
BigInteger fact2 = new BigInteger("1");
for (int j = 1; j <= cnt[i]; j++) {
b = b.multiply(new BigInteger(stack+""));
stack++;
fact2 = fact2.multiply(new BigInteger(j+""));
}
b = b.divide(fact2);
}
}
System.out.println(b);
}
}
}