-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordScape.java
More file actions
119 lines (104 loc) · 3.83 KB
/
Copy pathWordScape.java
File metadata and controls
119 lines (104 loc) · 3.83 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.*;
import java.io.*;
public class WordScape{
static char[] letters;
static HashSet<String> dictionary;
static int buckets = 100;
static LinkedList<String> permutations;
static LinkedList<String> solutions;
static void AllStrings(boolean[] used, int k, String prefix)
{
if (prefix.length()>=3 && !permutations.contains(prefix)){
permutations.add(prefix);
// System.out.println("prefix : "+ prefix);
}
// Base case: k is 0,
if (k == 0)
{
return;
}
// // One by one add all characters
// // from set and recursively
// // call for k equals to k-1
for (int i = 0; i < letters.length; ++i){
if (!used[i]){
used[i] = true;
AllStrings(used, k--, prefix+letters[i]);
used[i] = false;
}
}
// {
// // Next character of input added
// String newPrefix = prefix + set[i];
// // k is decreased, because
// // we have added a new character
// AllKLengthRec(set, newPrefix,
// n, k - 1);
// }
}
private static void printSol() throws FileNotFoundException{
// for (int i = 0; i < letters.length; i++){
// System.out.println(letters[i]);
// }
// import whole dictionary into hashmap
// initialize hm
dictionary = new HashSet<String>();
permutations = new LinkedList<String>();
solutions = new LinkedList<String>();
// FOR LATER MAKE THIS WORK REGARDLESS WHERE FILE IS
File dict = new File("/Users/roa/Desktop/gwu_stuff/personal/WordScape/dictionary.txt");
BufferedReader br = new BufferedReader(new FileReader(dict));
String word;
try {
// read in the file
while((word = br.readLine()) != null) {
// System.out.println(word + " hash = "+hash(word));
// hash each word
dictionary.add(word.toUpperCase());
}
} catch (IOException e) {
e.printStackTrace();
}
// try every combination of letters
// hash current combination
// look up in hashtablle
// if found add to a list of solutions
// System.out.println("\n\n"+dictionary.contains("rock"));
// String searchquery = "";
// for (int i = 0; i<letters.length; i++){
// searchquery+=letters.get(i);
// }
// System.out.println(searchquery);
// for (int i = 0; i<letters.length; i++){
boolean [] used = new boolean[letters.length];
// for (int i = 0; i < letters.length; ++i){
// used[i] = true;
AllStrings(used, letters.length, "");
// used[i] = false;
// }
for (int i = 0; i<permutations.size(); i++){
// System.out.println(permutations.get(i));
String curperm = permutations.get(i);
if (dictionary.contains(curperm)){
solutions.add(curperm);
}
}
for (int i = 0; i<solutions.size(); i++){
System.out.println(solutions.get(i));
}
}
public static void main (String[] argv) throws FileNotFoundException
{
if (argv.length==1){
// parse input
letters = new char[argv[0].length()];
// letters = new ArrayList<Character>()
for (int i = 0; i < argv[0].length(); i++){
letters[i] = argv[0].toUpperCase().charAt(i);
}
// solve for the given solution
printSol();
}
else System.out.println("please call as: WordScape <insert letters here> ");
}
}