Solving Boggle Backwards

Published on

This blog accompanies the video tutorial on building a Boggle solver that works in reverse… and does it surprisingly well (for smaller board sizes; it won’t be as effective for a 100x100 board). Instead of finding words on the board, it checks if each word can be formed from the board.

What You’ll Learn

Project Code

You’ll find the complete source code here: simple-boggle-solver

How It Works

The Idea

Most Boggle solvers start with the board and try to construct words by exploring every path. We’re doing the opposite: for each word in the dictionary, we check if it can be found on the board.

Step 1: Read the Board

We represent the board as a Dict[Position, str]. We pass it to our solver as a single string of length board_size * board_size

def build_board(board_str: str) -> Board:
    return {
        (i // BOARD_SIZE, i % BOARD_SIZE):
        board_str[i] for i in range(BOARD_SIZE * BOARD_SIZE)
    }

Step 2: Load the Dictionary

We use /usr/share/dict/words and filter out anything too short or capitalized or hyphenated

dictionary = [
    w.strip().lower()
    for w in f
    if len(w.strip()) > 2 and w.strip().isalpha() and w.strip().islower()
]

Step 3: Try Every Word

We scan each word in the dictionary and check if it can be formed on the board, using a recursive backtracking function that searches from every matching first letter.

def solve(board_str, dictionary):
    ...
    return [w for w in candidates if board_has_word(board, w)]

Step 4: Report Matches

Print all valid words found and sort by length or score.


External Resources

Stay in the Loop

Have questions or ideas for more coding adventures? Drop a comment on the video, or suggest the next project idea you want to reinvent!

Thanks for following along, and until next time. Keep reinventing!