forked from terrytong0876/LintCode-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckClock.java
More file actions
87 lines (66 loc) · 1.95 KB
/
CheckClock.java
File metadata and controls
87 lines (66 loc) · 1.95 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
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
85
86
87
import java.io.*;
import java.util.*;
/*
R.L.
12 spots to fill 4P, 4N, 4D. make sure they are not jumping onto each other.
P: move 1 step.
N: 5.
D: 10
*/
//
class Solution {
// Generate all valid 12-length strings
public static void generateAll(ArrayList<String> solutions, String current, int p, int n, int d) {
if (current.length() == 12) {
solutions.add(current);
return;
}
if (p > 0) generateAll(solutions, current + "P", p - 1, n, d);
if (n > 0) generateAll(solutions, current + "N", p, n - 1, d);
if (d > 0) generateAll(solutions, current + "D", p, n, d - 1);
}
// Return true whether the string solution is valid, or false otehrwise
public static boolean isValid(String solution) {
if (solution == null || solution.length() != 12)
return false;
boolean[] clock = new boolean[12];
int pointer = 0;
for (int c = 0; c < 12; c++) {
if (clock[pointer])
return false;
int advance = 0;
switch (solution.charAt(c)) {
case 'P': advance = 1; break;
case 'N': advance = 5; break;
case 'D': advance = 10; break;
}
clock[pointer] = true;
pointer = (pointer + advance) % 12;
}
for (int c = 0; c < 12; c++) {
if (!clock[c])
return false;
}
return true;
}
// Generate all valid strings, then filter them out and only print the valid ones
public static void printSolution() {
ArrayList<String> solutions = new ArrayList<String>();
generateAll(solutions, "", 4, 4, 4);
for (String s : solutions) {
if (isValid(s))
System.out.println(s);
}
}
public static void main(String[] args) {
printSolution();
/*
ArrayList<String> rst = validateClock();
System.out.println(rst.size());
for (String string : rst) {
System.out.println(string);
}
*/
}
}
///Generate all possible solutions, then validate them all.