![]() |
VOOZH | about |
Given a dictionary, a method to do a lookup in the dictionary and a M x N board where every cell has one character. Find all possible words that can be formed by a sequence of adjacent characters. Note that we can move to any of 8 adjacent characters, but a word should not have multiple instances of the same cell.
Example:
Input: dictionary[] = {"GEEKS", "FOR", "QUIZ", "GO"}; boggle[][] = {{'G', 'I', 'Z'}, {'U', 'E', 'K'}, {'Q', 'S', 'E'}}; Output: Following words of the dictionary are present GEEKS QUIZ Explanation:
Input: dictionary[] = {"GEEKS", "ABCFIHGDE"}; boggle[][] = {{'A', 'B', 'C'}, {'D', 'E', 'F'}, {'G', 'H', 'I'}}; Output: Following words of the dictionary are present ABCFIHGDE Explanation:
We have discussed a Graph DFS based solution in below post.
Boggle (Find all possible words in a board of characters) | Set 1
Here we discuss a Trie based solution which is better than DFS based solution.
Given Dictionary dictionary[] = {"GEEKS", "FOR", "QUIZ", "GO"}
1. Create an Empty trie and insert all words of given dictionary into trie
After insertion, Trie looks like(leaf nodes are in RED)
root
/
G F Q
/ | | |
O E O U
| | |
E R I
| |
K Z
|
S
2. After that we have pick only those character in boggle[][] which are child of root of Trie
Let for above we pick 'G' boggle[0][0], 'Q' boggle[2][0] (they both are present in boggle matrix)
3. search a word in a trie which start with character that we pick in step 2
1) Create bool visited boolean matrix (Visited[M][N] = false ) 2) Call SearchWord() for every cell (i, j) which has one of the first characters of dictionary words. In above example, we have 'G' and 'Q' as first characters. SearchWord(Trie *root, i, j, visited[][N]) if root->leaf == true print word if we have seen this element first time then make it visited. visited[i][j] = true do traverse all child of current root k goes (0 to 26 ) [there are only 26 Alphabet] add current char and search for next character find next character which is adjacent to boggle[i][j] they are 8 adjacent cells of boggle[i][j] (i+1, j+1), (i+1, j) (i-1, j) and so on. make it unvisited visited[i][j] = false
Below is the implementation of above idea:
GEE GEEKS QUIZ
Complexity Analysis:
W is the number of words and L is the maximum word length. From each board cell (B = N²), DFS explores up to 8 directions with depth bounded by L, giving O(8^L) per cell in the worst case.L.GEE GEEKS QUIZ
Time Complexity: O(W · B · 8^L)
Space Complexity: O(W + L)