forked from kdn251/interviews
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MischievousChildren.java
executable file
·84 lines (76 loc) · 2.85 KB
/
MischievousChildren.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Adam’s parents put up a sign that says “CONGRATULATIONS”. The sign is so big that exactly one
* letter fits on each panel. Some of Adam’s younger cousins got bored during the reception and decided
* to rearrange the panels. How many unique ways can the panels be arranged (counting the original
* arrangement)?
* Input
* The first line of input is a single non-negative integer. It indicates the number of data sets to follow.
* Its value will be less than 30001.
* Each data set consists of a single word, in all capital letters.
* Each word will have at most 20 letters. There will be no spaces or other punctuation.
* The number of arrangements will always be able to fit into an unsigned long int. Note that 12!
* is the largest factorial that can fit into an unsigned long int.
* Output
* For each word, output the number of unique ways that the letters can be rearranged (counting the
* original arrangement). Use the format shown in Sample Output, below.
* Sample Input
* 3
* HAPPY
* WEDDING
* ADAM
* Sample Output
* Data set 1: 60
* Data set 2: 2520
* Data set 3: 12
*/
//https://uva.onlinejudge.org/index.php?option=onlinejudge&Itemid=99999999&page=show_problem&category=&problem=1279
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class MischievousChildren {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int numberOfCases = input.nextInt();
int currentCase = 1;
while (numberOfCases != 0) {
String line = input.next();
int numberOfLetters = line.length();
Map<Character, Integer> letterCounter = new HashMap<Character, Integer>();
for (int i = 0; i < numberOfLetters; i++) {
char c = line.charAt(i);
if (letterCounter.containsKey(c)) {
int previousOccurrences = letterCounter.get(c);
letterCounter.replace(c, previousOccurrences + 1);
} else {
letterCounter.put(c, 1);
}
}
String lineWithoutDuplicates = "";
for (int i = 0; i < numberOfLetters; i++) {
char c = line.charAt(i);
if (!lineWithoutDuplicates.contains(c + "")) {
lineWithoutDuplicates = lineWithoutDuplicates + c;
}
}
long nFactorial = computeFactorial(numberOfLetters);
for (int i = 0; i < letterCounter.size(); i++) {
char c = lineWithoutDuplicates.charAt(i);
int numberOfOccurrences = letterCounter.get(c);
if (numberOfOccurrences != 1) {
long currentProduct = computeFactorial(numberOfOccurrences);
nFactorial = nFactorial / currentProduct;
}
}
System.out.println("Data set " + currentCase + ": " + nFactorial);
currentCase++;
numberOfCases--;
}
}
private static long computeFactorial(int number) {
long product = 1;
for (int i = 2; i < number + 1; i++) {
product = product * i;
}
return product;
}
}