Problem Statement |
| Let's consider a standard six-sided die. Each side contains a distinct number between 1 and 6. We can represent a single die as a sequence of 6 digits in the following order: the number on its top side, bottom side, left, right, front and back sides. You are given a String[] givenDice, each element of which represents a single die in the described format.
Your task is to determine the number of distinct dice in givenDice. Two dice are considered equal if they can be rotated in such a way that the numbers on the corresponding sides are all equal.
|
Definition |
| Class: | DistinctDice | Method: | getDistinct | Parameters: | String[] | Returns: | int | Method signature: | int getDistinct(String[] givenDice) | (be sure your method is public) |
|
|
|
Constraints |
- | givenDice will contain between 1 and 50 elements, inclusive. |
- | Each element of givenDice will contain exactly 6 characters. |
- | Each character of each element of givenDice will be a digit between '1' and '6', inclusive. |
- | Within each element of givenDice, all characters will be distinct. |
Examples |
0) | |
| |
1) | |
| {"145326","154236","216543"}
|
| Returns: 3
| |
|
2) | |
| |
3) | |
| {"546231", "245631", "531462", "524631", "614235", "415623", "423651", "316254", "432165", "316452", "135426", "643512"}
|
| Returns: 10
| |
|
import java.util.*;
public class DistinctDice {
Set<String> dice = new HashSet<String>();
int[] lt = {2, 3, 1, 0, 4, 5};
int[] lf = {0, 1, 5, 4, 2, 3};
public int getDistinct(String[] givenDice) {
int sum = 0;
for (String s : givenDice)
if (!dice.contains(s)) {
rotate(s);
sum++;
}
return sum;
}
private void rotate(String s) {
if (dice.contains(s))
return;
dice.add(s);
char[] c1 = new char[6];
char[] c2 = new char[6];
for (int i = 0; i < 6; i++) {
c1[i] = s.charAt(lt[i]);
c2[i] = s.charAt(lf[i]);
}
rotate(new String(c1));
rotate(new String(c2));
}
}