-
Notifications
You must be signed in to change notification settings - Fork 19.4k
/
WordBoggle.java
125 lines (103 loc) · 3.45 KB
/
WordBoggle.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
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
package com.thealgorithms.misc;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
public final class WordBoggle {
private WordBoggle() {
}
/**
* O(nm * 8^s + ws) time where n = width of boggle board, m = height of
* boggle board, s = length of longest word in string array, w = length of
* string array, 8 is due to 8 explorable neighbours O(nm + ws) space.
*/
public static List<String> boggleBoard(char[][] board, String[] words) {
Trie trie = new Trie();
for (String word : words) {
trie.add(word);
}
Set<String> finalWords = new HashSet<>();
boolean[][] visited = new boolean[board.length][board.length];
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
explore(i, j, board, trie.root, visited, finalWords);
}
}
return new ArrayList<>(finalWords);
}
public static void explore(int i, int j, char[][] board, TrieNode trieNode, boolean[][] visited, Set<String> finalWords) {
if (visited[i][j]) {
return;
}
char letter = board[i][j];
if (!trieNode.children.containsKey(letter)) {
return;
}
visited[i][j] = true;
trieNode = trieNode.children.get(letter);
if (trieNode.children.containsKey('*')) {
finalWords.add(trieNode.word);
}
List<Integer[]> neighbors = getNeighbors(i, j, board);
for (Integer[] neighbor : neighbors) {
explore(neighbor[0], neighbor[1], board, trieNode, visited, finalWords);
}
visited[i][j] = false;
}
public static List<Integer[]> getNeighbors(int i, int j, char[][] board) {
List<Integer[]> neighbors = new ArrayList<>();
if (i > 0 && j > 0) {
neighbors.add(new Integer[] {i - 1, j - 1});
}
if (i > 0 && j < board[0].length - 1) {
neighbors.add(new Integer[] {i - 1, j + 1});
}
if (i < board.length - 1 && j < board[0].length - 1) {
neighbors.add(new Integer[] {i + 1, j + 1});
}
if (i < board.length - 1 && j > 0) {
neighbors.add(new Integer[] {i + 1, j - 1});
}
if (i > 0) {
neighbors.add(new Integer[] {i - 1, j});
}
if (i < board.length - 1) {
neighbors.add(new Integer[] {i + 1, j});
}
if (j > 0) {
neighbors.add(new Integer[] {i, j - 1});
}
if (j < board[0].length - 1) {
neighbors.add(new Integer[] {i, j + 1});
}
return neighbors;
}
}
// Trie used to optimize string search
class TrieNode {
Map<Character, TrieNode> children = new HashMap<>();
String word = "";
}
class Trie {
TrieNode root;
char endSymbol;
Trie() {
this.root = new TrieNode();
this.endSymbol = '*';
}
public void add(String str) {
TrieNode node = this.root;
for (int i = 0; i < str.length(); i++) {
char letter = str.charAt(i);
if (!node.children.containsKey(letter)) {
TrieNode newNode = new TrieNode();
node.children.put(letter, newNode);
}
node = node.children.get(letter);
}
node.children.put(this.endSymbol, null);
node.word = str;
}
}