VOOZH about

URL: https://www.geeksforgeeks.org/dsa/boggle-using-trie/

⇱ Boggle using Trie - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Boggle using Trie

Last Updated : 15 Jan, 2026

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:

👁 Image

Input: dictionary[] = {"GEEKS", "ABCFIHGDE"};
 boggle[][] = {{'A', 'B', 'C'},
 {'D', 'E', 'F'},
 {'G', 'H', 'I'}};
Output: Following words of the dictionary are present
 ABCFIHGDE 
Explanation:

👁 Image

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: 


Output
GEE
GEEKS
QUIZ

Complexity Analysis:

  • Time complexity: O(W · L + B · 8^L). 
    Building the trie takes O(W · L), where 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.
  • Auxiliary Space: O(W · L + B). 
    Trie storage takes O(W · L) space, the visited matrix uses O(B) space, and the recursion stack is bounded by the word length L.

OPTIMIZED APPROACH WITHOUT USING TRIE ( Short and Easy to understand with a better Time and Space complexity):

  1. First, we create a Set Data Structure and add all word in it to avoid duplicate word.
  2. Then we make a new array of type String and add those set elements to it.
  3. We check word by word using a for loop whether that particular word is present in the board and if it returns true , we shall add it our ArrayList<String>.
  4. While searching for a word in the board,we shall use backtracking so that while coming back,we can alter the board as it was before.
  5. Lastly we sort the array and print it.

Output
GEE GEEKS QUIZ 

Time Complexity: O(W · B · 8^L)
Space Complexity: O(W + L)

Comment