-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolver.java
65 lines (65 loc) · 2.15 KB
/
Solver.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
package FX.FXWordLink;
import java.io.IOException;
import java.util.*;
public class Solver {
static Scanner sc = new Scanner(System.in);
static java.util.Dictionary dict;
public static List<String> getAllWords(String chars, int length) throws IOException{
String[] letters = chars.replaceAll("[^A-Za-z]", "").toUpperCase().split("");
int numPossible = (int)(Math.pow(letters.length, length));
List<String> possible = new ArrayList<String>();
for (int i = 0; i < numPossible; i++) {
int i1 = Integer.parseInt(Integer.toString(i, letters.length));
String i2 = String.format("%0"+length+"d", i1);
String str = convert(letters,intArray(i2.split("")));
if (!possible.contains(str) && allValidLetters(str,letters))
possible.add(str.toLowerCase());
}
Collections.sort(possible);
noCopies(possible);
compareWithDict(possible,new Dictionary().getDict());
return possible;
}
private static void compareWithDict(List<String> possible, List<String> list) {
List<String> valid = new ArrayList<String>();
for (String x: possible)
if (list.contains(x))
valid.add(x);
possible.clear();
possible.addAll(valid);
}
public static List<String> getAllWords (String chars) throws IOException {
return getAllWords(chars,chars.length());
}
public static void noCopies (List<String> strings) {
List<String> newList = new ArrayList<String>();
for (String s: strings)
if (!newList.contains(s))
newList.add(s);
strings.clear();
strings.addAll(newList);
}
private static boolean allValidLetters(String str, String[] letters) {
int[] numOfEachLetter = new int[26];
for (String s: letters)
numOfEachLetter[(s.charAt(0))-65]++;
for (int i = 0; i < str.length(); i++)
numOfEachLetter[(str.charAt(i))-65]--;
for (int i: numOfEachLetter)
if (i < 0)
return false;
return true;
}
private static String convert(String[] letters, int[] intArray) {
String str = "";
for (int i = 0; i < intArray.length; i++)
str += letters[intArray[i]];
return str;
}
private static int[] intArray(String[] split) {
int[] arr = new int[split.length];
for (int i = 0; i < arr.length; i++)
arr[i] = Integer.parseInt(split[i]);
return arr;
}
}