-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordPuzzle.java
More file actions
192 lines (177 loc) · 7.82 KB
/
WordPuzzle.java
File metadata and controls
192 lines (177 loc) · 7.82 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
public class WordPuzzle{
public static void main(String[] args) throws Exception{
//Taking user input for rows and columns
Scanner sc = new Scanner(System.in);
System.out.println("Please input a value for the rows of the grid in range of (10 - 40): ");
int rows = sc.nextInt();
if(rows <10 || rows>40){
sc.close();
throw new Exception("Invalid Input");
}
System.out.println("Please input a value for the columns of the grid in range of (10 - 40): ");
int columns = sc.nextInt();
if(columns <10 || columns>40){
sc.close();
throw new Exception("Invalid Input");
}
char a[][] = new char[10000][10000];
sc.close();
//Declating Hash table , Helper hashmap and final map which will store words
QuadraticProbingHashTable<String> qpht = new QuadraticProbingHashTable<String>();
HashMap<String, Integer> finalMap = new HashMap<>();
HashMap<String, Integer> helperHash = new HashMap<>();
//creating a random grid
System.out.println("Created grid of random characters: ");
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
a[i][j] = (char) (Math.random() * 26 + 'a');
System.out.print(a[i][j] + " ");
}
System.out.print('\n');
}
try{
//Reading words from dictionary
FileReader fr = new FileReader("dictionary.txt");
BufferedReader bufferedReader = new BufferedReader(fr);
String dictWords;
while((dictWords = bufferedReader.readLine()) != null){
String currWord = new String();
currWord = "";
//storing each prefix of the word for enhancement
for(int i = 0; i < dictWords.length(); i++){
currWord = currWord + String.valueOf(dictWords.charAt(i));
if(helperHash.get(currWord) == null){
//hashmap storing all prefix for later use
helperHash.put(currWord,1);
}
}
//addding word in dictionary
qpht.insert(dictWords);
}
bufferedReader.close();
}catch(IOException e){
System.out.println("File not found");
}
//Starting to calculate running time after creating table
long startTime = System.nanoTime();
//read words form the grid and write them into a array list
ArrayList<String> gridWordList = new ArrayList<>();
ArrayList<String> finalList = new ArrayList<>();
String newWord = new String();
//Going through all possible combination of words
for(int i = 0; i < rows; i++){
for(int j = 0; j < columns; j++){
newWord = "";
for(int m = j; m >= 0; m--){
newWord = newWord + String.valueOf(a[i][m]);
//Check if prefix is present in hashmap
if (helperHash.get(newWord) == null){
break;
}
gridWordList.add(newWord);
}
newWord = String.valueOf(a[i][j]);
//Check if prefix is present in hashmap
if (helperHash.get(newWord) == null){
break;
}
for(int m = j + 1; m < columns; m++){
newWord = newWord + String.valueOf(a[i][m]);
//Check if prefix is present in hashmap
if (helperHash.get(newWord) == null){
break;
}
gridWordList.add(newWord);
}
newWord = String.valueOf(a[i][j]);
for(int m = i - 1; m >= 0; m--){
newWord = newWord + String.valueOf(a[m][j]);
//Check if prefix is present in hashmap
if (helperHash.get(newWord) == null){
break;
}
gridWordList.add(newWord);
}
newWord = String.valueOf(a[i][j]);
for(int m = i + 1; m < rows; m++){
newWord = newWord + String.valueOf(a[m][j]);
//Check if prefix is present in hashmap
if (helperHash.get(newWord) == null){
break;
}
gridWordList.add(newWord);
}
newWord = String.valueOf(a[i][j]);
for(int q = i - 1, r = j - 1; q >= 0 && r >= 0; q--, r--){
newWord = newWord + String.valueOf(a[q][r]);
//Check if prefix is present in hashmap
if(helperHash.get(newWord) == null){
break;
}
gridWordList.add(newWord);
}
newWord = String.valueOf(a[i][j]);
for(int q = i - 1, r = j + 1; q >= 0 && r < columns; q--, r++){
newWord = newWord + String.valueOf(a[q][r]);
//Check if prefix is present in hashmap
if (helperHash.get(newWord) == null){
break;
}
gridWordList.add(newWord);
}
newWord = String.valueOf(a[i][j]);
for(int q = i + 1, r = j - 1; q < rows && r >= 0; q++, r--){
newWord = newWord + String.valueOf(a[q][r]);
//Check if prefix is present in hashmap
if (helperHash.get(newWord) == null){
break;
}
gridWordList.add(newWord);
}
newWord = String.valueOf(a[i][j]);
for(int q = i + 1, r = j + 1; q < rows && r < columns; q++, r++){
newWord = newWord + String.valueOf(a[q][r]);
//Check if prefix is present in hashmap
if (helperHash.get(newWord) == null){
break;
}
gridWordList.add(newWord);
}
}
}
//output the words
System.out.println('\n' + "Words in this grid after enhancement");
for(int m = 0; m < gridWordList.size(); m++){
System.out.println(gridWordList.get(m));
}
System.out.println('\n' + "Words present in the dictionary :\n");
//check if any of the words exist in the grid by searching hash table
for(int m = 0; m < gridWordList.size(); m++){
if(qpht.contains(gridWordList.get(m)) && finalMap.get(gridWordList.get(m)) == null){
//adding words in final list and avoiding duplicates
finalList.add(gridWordList.get(m));
finalMap.put(gridWordList.get(m), 1);
System.out.println("Searched word from hash table dictionary is: " + gridWordList.get(m));
}
}
//sorting the final list
Collections.sort(finalList);
System.out.println("Dictionary words after sorting:");
for(String counter: finalList){
System.out.println(counter);
}
long endTime = System.nanoTime();
long totalTime = endTime - startTime;
double elapsedTimeInSecond = (double) totalTime / 1_000_000_000;
//calculating the total runtime of program and printing
System.out.println("Execution time is " + elapsedTimeInSecond + " seconds");
System.out.println("END");
}
}