|
2 | 2 |
|
3 | 3 | public class WordBoggle { |
4 | 4 |
|
5 | | - /** |
6 | | - * O(nm * 8^s + ws) time where n=width of boggle board, m=height of boggle board, s=length of |
7 | | - * longest word in string array, w= length of string array, 8 is due to 8 explorable neighbours |
8 | | - * O(nm + ws) space. |
9 | | - */ |
10 | | - public static List<String> boggleBoard(char[][] board, String[] words) { |
11 | | - Trie trie = new Trie(); |
12 | | - for (String word : words) trie.add(word); |
13 | | - Set<String> finalWords = new HashSet<>(); |
14 | | - boolean[][] visited = new boolean[board.length][board.length]; |
15 | | - for (int i = 0; i < board.length; i++) |
16 | | - for (int j = 0; j < board[i].length; j++) |
17 | | - explore(i, j, board, trie.root, visited, finalWords); |
18 | | - return new ArrayList<>(finalWords); |
| 5 | + /** |
| 6 | + * O(nm * 8^s + ws) time where n=width of boggle board, m=height of boggle board, s=length of |
| 7 | + * longest word in string array, w= length of string array, 8 is due to 8 explorable neighbours |
| 8 | + * O(nm + ws) space. |
| 9 | + */ |
| 10 | + public static List<String> boggleBoard(char[][] board, String[] words) { |
| 11 | + Trie trie = new Trie(); |
| 12 | + for (String word : words) trie.add(word); |
| 13 | + Set<String> finalWords = new HashSet<>(); |
| 14 | + boolean[][] visited = new boolean[board.length][board.length]; |
| 15 | + for (int i = 0; i < board.length; i++) |
| 16 | + for (int j = 0; j < board[i].length; j++) |
| 17 | + explore(i, j, board, trie.root, visited, finalWords); |
| 18 | + return new ArrayList<>(finalWords); |
| 19 | + } |
| 20 | + |
| 21 | + public static void main(String[] args) { |
| 22 | + // Testcase |
| 23 | + List<String> ans = |
| 24 | + new ArrayList<>( |
| 25 | + Arrays.asList("a", "boggle", "this", "NOTRE_PEATED", "is", "simple", "board")); |
| 26 | + assert (boggleBoard( |
| 27 | + new char[][] { |
| 28 | + {'t', 'h', 'i', 's', 'i', 's', 'a'}, |
| 29 | + {'s', 'i', 'm', 'p', 'l', 'e', 'x'}, |
| 30 | + {'b', 'x', 'x', 'x', 'x', 'e', 'b'}, |
| 31 | + {'x', 'o', 'g', 'g', 'l', 'x', 'o'}, |
| 32 | + {'x', 'x', 'x', 'D', 'T', 'r', 'a'}, |
| 33 | + {'R', 'E', 'P', 'E', 'A', 'd', 'x'}, |
| 34 | + {'x', 'x', 'x', 'x', 'x', 'x', 'x'}, |
| 35 | + {'N', 'O', 'T', 'R', 'E', '_', 'P'}, |
| 36 | + {'x', 'x', 'D', 'E', 'T', 'A', 'E'}, |
| 37 | + }, |
| 38 | + new String[] { |
| 39 | + "this", |
| 40 | + "is", |
| 41 | + "not", |
| 42 | + "a", |
| 43 | + "simple", |
| 44 | + "test", |
| 45 | + "boggle", |
| 46 | + "board", |
| 47 | + "REPEATED", |
| 48 | + "NOTRE_PEATED", |
| 49 | + }) |
| 50 | + .equals(ans)); |
| 51 | + } |
| 52 | + |
| 53 | + public static void explore( |
| 54 | + int i, |
| 55 | + int j, |
| 56 | + char[][] board, |
| 57 | + TrieNode trieNode, |
| 58 | + boolean[][] visited, |
| 59 | + Set<String> finalWords) { |
| 60 | + if (visited[i][j]) return; |
| 61 | + |
| 62 | + char letter = board[i][j]; |
| 63 | + if (!trieNode.children.containsKey(letter)) { |
| 64 | + return; |
19 | 65 | } |
| 66 | + visited[i][j] = true; |
| 67 | + trieNode = trieNode.children.get(letter); |
| 68 | + if (trieNode.children.containsKey('*')) finalWords.add(trieNode.word); |
20 | 69 |
|
21 | | - public static void main(String[] args) { |
22 | | - // Testcase |
23 | | - List<String> ans = |
24 | | - new ArrayList<>( |
25 | | - Arrays.asList("a", "boggle", "this", "NOTRE_PEATED", "is", "simple", "board")); |
26 | | - assert (boggleBoard( |
27 | | - new char[][] { |
28 | | - {'t', 'h', 'i', 's', 'i', 's', 'a'}, |
29 | | - {'s', 'i', 'm', 'p', 'l', 'e', 'x'}, |
30 | | - {'b', 'x', 'x', 'x', 'x', 'e', 'b'}, |
31 | | - {'x', 'o', 'g', 'g', 'l', 'x', 'o'}, |
32 | | - {'x', 'x', 'x', 'D', 'T', 'r', 'a'}, |
33 | | - {'R', 'E', 'P', 'E', 'A', 'd', 'x'}, |
34 | | - {'x', 'x', 'x', 'x', 'x', 'x', 'x'}, |
35 | | - {'N', 'O', 'T', 'R', 'E', '_', 'P'}, |
36 | | - {'x', 'x', 'D', 'E', 'T', 'A', 'E'}, |
37 | | - }, |
38 | | - new String[] { |
39 | | - "this", |
40 | | - "is", |
41 | | - "not", |
42 | | - "a", |
43 | | - "simple", |
44 | | - "test", |
45 | | - "boggle", |
46 | | - "board", |
47 | | - "REPEATED", |
48 | | - "NOTRE_PEATED", |
49 | | - }) |
50 | | - .equals(ans)); |
51 | | - } |
| 70 | + List<Integer[]> neighbors = getNeighbors(i, j, board); |
| 71 | + for (Integer[] neighbor : neighbors) |
| 72 | + explore(neighbor[0], neighbor[1], board, trieNode, visited, finalWords); |
52 | 73 |
|
53 | | - public static void explore( |
54 | | - int i, |
55 | | - int j, |
56 | | - char[][] board, |
57 | | - TrieNode trieNode, |
58 | | - boolean[][] visited, |
59 | | - Set<String> finalWords) { |
60 | | - if (visited[i][j]) return; |
61 | | - |
62 | | - char letter = board[i][j]; |
63 | | - if (!trieNode.children.containsKey(letter)) { |
64 | | - return; |
65 | | - } |
66 | | - visited[i][j] = true; |
67 | | - trieNode = trieNode.children.get(letter); |
68 | | - if (trieNode.children.containsKey('*')) finalWords.add(trieNode.word); |
69 | | - |
70 | | - List<Integer[]> neighbors = getNeighbors(i, j, board); |
71 | | - for (Integer[] neighbor : neighbors) |
72 | | - explore(neighbor[0], neighbor[1], board, trieNode, visited, finalWords); |
73 | | - |
74 | | - visited[i][j] = false; |
75 | | - } |
| 74 | + visited[i][j] = false; |
| 75 | + } |
76 | 76 |
|
77 | | - public static List<Integer[]> getNeighbors(int i, int j, char[][] board) { |
78 | | - List<Integer[]> neighbors = new ArrayList<>(); |
79 | | - if (i > 0 && j > 0) neighbors.add(new Integer[] {i - 1, j - 1}); |
| 77 | + public static List<Integer[]> getNeighbors(int i, int j, char[][] board) { |
| 78 | + List<Integer[]> neighbors = new ArrayList<>(); |
| 79 | + if (i > 0 && j > 0) neighbors.add(new Integer[] {i - 1, j - 1}); |
80 | 80 |
|
81 | | - if (i > 0 && j < board[0].length - 1) neighbors.add(new Integer[] {i - 1, j + 1}); |
| 81 | + if (i > 0 && j < board[0].length - 1) neighbors.add(new Integer[] {i - 1, j + 1}); |
82 | 82 |
|
83 | | - if (i < board.length - 1 && j < board[0].length - 1) |
84 | | - neighbors.add(new Integer[] {i + 1, j + 1}); |
| 83 | + if (i < board.length - 1 && j < board[0].length - 1) |
| 84 | + neighbors.add(new Integer[] {i + 1, j + 1}); |
85 | 85 |
|
86 | | - if (i < board.length - 1 && j > 0) neighbors.add(new Integer[] {i + 1, j - 1}); |
| 86 | + if (i < board.length - 1 && j > 0) neighbors.add(new Integer[] {i + 1, j - 1}); |
87 | 87 |
|
88 | | - if (i > 0) neighbors.add(new Integer[] {i - 1, j}); |
| 88 | + if (i > 0) neighbors.add(new Integer[] {i - 1, j}); |
89 | 89 |
|
90 | | - if (i < board.length - 1) neighbors.add(new Integer[] {i + 1, j}); |
| 90 | + if (i < board.length - 1) neighbors.add(new Integer[] {i + 1, j}); |
91 | 91 |
|
92 | | - if (j > 0) neighbors.add(new Integer[] {i, j - 1}); |
| 92 | + if (j > 0) neighbors.add(new Integer[] {i, j - 1}); |
93 | 93 |
|
94 | | - if (j < board[0].length - 1) neighbors.add(new Integer[] {i, j + 1}); |
| 94 | + if (j < board[0].length - 1) neighbors.add(new Integer[] {i, j + 1}); |
95 | 95 |
|
96 | | - return neighbors; |
97 | | - } |
| 96 | + return neighbors; |
| 97 | + } |
98 | 98 | } |
99 | 99 |
|
100 | 100 | // Trie used to optimize string search |
101 | 101 | class TrieNode { |
102 | 102 |
|
103 | | - Map<Character, TrieNode> children = new HashMap<>(); |
104 | | - String word = ""; |
| 103 | + Map<Character, TrieNode> children = new HashMap<>(); |
| 104 | + String word = ""; |
105 | 105 | } |
106 | 106 |
|
107 | 107 | class Trie { |
108 | 108 |
|
109 | | - TrieNode root; |
110 | | - char endSymbol; |
111 | | - |
112 | | - public Trie() { |
113 | | - this.root = new TrieNode(); |
114 | | - this.endSymbol = '*'; |
115 | | - } |
116 | | - |
117 | | - public void add(String str) { |
118 | | - TrieNode node = this.root; |
119 | | - for (int i = 0; i < str.length(); i++) { |
120 | | - char letter = str.charAt(i); |
121 | | - if (!node.children.containsKey(letter)) { |
122 | | - TrieNode newNode = new TrieNode(); |
123 | | - node.children.put(letter, newNode); |
124 | | - } |
125 | | - node = node.children.get(letter); |
126 | | - } |
127 | | - node.children.put(this.endSymbol, null); |
128 | | - node.word = str; |
| 109 | + TrieNode root; |
| 110 | + char endSymbol; |
| 111 | + |
| 112 | + public Trie() { |
| 113 | + this.root = new TrieNode(); |
| 114 | + this.endSymbol = '*'; |
| 115 | + } |
| 116 | + |
| 117 | + public void add(String str) { |
| 118 | + TrieNode node = this.root; |
| 119 | + for (int i = 0; i < str.length(); i++) { |
| 120 | + char letter = str.charAt(i); |
| 121 | + if (!node.children.containsKey(letter)) { |
| 122 | + TrieNode newNode = new TrieNode(); |
| 123 | + node.children.put(letter, newNode); |
| 124 | + } |
| 125 | + node = node.children.get(letter); |
129 | 126 | } |
| 127 | + node.children.put(this.endSymbol, null); |
| 128 | + node.word = str; |
| 129 | + } |
130 | 130 | } |
0 commit comments