-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacktracking.java
More file actions
executable file
·58 lines (49 loc) · 1.65 KB
/
Copy pathBacktracking.java
File metadata and controls
executable file
·58 lines (49 loc) · 1.65 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
import java.util.*;
import java.util.LinkedList;
import java.util.List;
public class Backtracking{
public static List<Integer> sumOfDistinctCubes(int n ){
List<Integer> cubesList = new ArrayList<Integer>();
sumOfDistinctCubes(n , (int) Math.cbrt(n), cubesList);
return cubesList;
}
private static boolean sumOfDistinctCubes(int n , int c, List<Integer>soFar) {
if(n == 0){
return true;
}
if(c == 0){
return false;
}
for(int i = c;i > 0;i--) {
if(n >= (i*i*i)) {
soFar.add(i);
if(sumOfDistinctCubes(n-(i*i*i) , i-1 , soFar)){
return true;
}
soFar.remove(soFar.size()-1);
}
}
return false;
}
public static List<String> forbiddenSubstrings(String alphabet, int n, List<String> tabu) {
ArrayList<String> result = new ArrayList<String>();
forbiddenSubstrings(alphabet, n, tabu, "", result);
return result;
}
private static void forbiddenSubstrings(String alphabet, int n, List<String> tabu, String soFar, List<String> result) {
for (int i = 0; i < tabu.size(); i++) {
if (soFar.endsWith(tabu.get(i))) {
return;
}
}
if (soFar.length() == n) {
result.add(soFar);
}
else {
for (int i = 0; i < alphabet.length(); i++) {
char ch = alphabet.charAt(i);
forbiddenSubstrings(alphabet, n, tabu, soFar + ch, result);
}
}
}
}