Prepin
Log in

Identify every potential word for a game of Boggle.

Approach / Explanation

This task involves creating a program that identifies all potential words that can be generated on a Boggle board. Boggle is a word game played on a 4x4 grid filled with letters, where players try to find words by linking adjacent letters. The words must be formed from letters in cells that are sequentially adjacent, meaning they are directly next to each other either horizontally or vertically. A cell containing a letter cannot be used more than once in forming a word. To effectively tackle this problem, consider the following key steps: Establish a 2D array representing the Boggle board and a dictionary of acceptable words. Explore each cell on the board. Perform a depth-first search (DFS) in all 8 possible adjacent directions. If the current path's prefix corresponds to a word in the dictionary, record that word. Continue following these steps until all cells on the board have been explored.

Suggested Answer

The provided solution employs a straightforward algorithm to generate all valid words from a specified Boggle board. It examines each cell of the Boggle board and executes a Depth-First Search (DFS) starting from each individual cell.

Alternative Answer

```python from typing import List trie = {} END = '$' class Solution: def find(self, board: List[List[int]], dictionary: List[int]) -> List[str]: for word in dictionary: node = trie for letter in word: node = node.setdefault(letter, {}) node[END] = word row, col = len(board), len(board[0]) visited = [[0 for _ in range(col)] for _ in range(row)] res = [] def dfs(i, j, t): if END in t: res.append(t[END]) del t[END] visited[i][j] = 1 points = [(i + dx, j + dy) for dx, dy in [(-1, 0), (1, 0), (0, -1), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1)] if 0 <= i + dx < row and 0 <= j + dy < col] for dx, dy in points: if board[dx][dy] in t and not visited[dx][dy]: dfs(dx, dy, t[board[dx][dy]]) visited[i][j] = 0 for i in range(row): for j in range(col): if board[i][j] in trie: dfs(i, j, trie[board[i][j]]) return res board = [['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H'], ['I', 'J', 'K', 'L'], ['M', 'N', 'O', 'P']] word_dict = {"ABE", "ACF", "ACFH", "ACFHJ", "ADI", "ABCFH", "BCFH", "CFH"} sol = Solution() print(sol.find(board, word_dict)) ``` In this example, the code utilizes a Trie data structure to facilitate efficient word retrieval, alongside a Depth-First Search (DFS) method for navigating the board. The integration of Trie and DFS allows for enhanced performance when working with complex datasets in this problem.

Question Details

Difficulty & Category

Medium

Companies

WhatsApp

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.