Validating a boggle word using JavaScript

A Boggle board is a 2D array of individual characters. We need to validate whether a given word can be formed by connecting adjacent cells (horizontally, vertically, or diagonally) without reusing any previously used cells.

Problem

Given a Boggle board like this:

const board = [
    ["I","L","A","W"],
    ["B","N","G","E"],
    ["I","U","A","O"],
    ["A","S","R","L"]
];

We need to check if words like "BINGO" or "LINGO" are valid. Valid words are formed by moving to adjacent cells without reusing any cell in the current path.

Algorithm Overview

The solution uses a breadth-first search (BFS) approach:

  • Find all starting positions that match the first letter

  • Use a queue to explore all possible paths

  • For each position, check all 8 adjacent directions

  • Track visited cells to prevent reuse within the same path

Implementation

const board = [
    ["I","L","A","W"],
    ["B","N","G","E"],
    ["I","U","A","O"],
    ["A","S","R","L"]
];

const checkWord = (board = [], guess = '') => {
    const numRows = board.length;
    const numCols = board[0].length;
    
    // Find all starting positions matching first letter
    let queue = board.reduce((acc, row, i) => {
        row.forEach((x, j) => {
            if (x === guess[0]) {
                acc.push({ pos: {r: i, c: j}, nextIndex: 1, path: [numCols*i + j] });
            }
        });
        return acc;
    }, []);
    
    // Explore adjacent cells for next letter
    let exploreWord = (obj, queue) => {
        let allMoves = [
            {r: obj.pos.r - 1, c: obj.pos.c},     // up
            {r: obj.pos.r + 1, c: obj.pos.c},     // down
            {r: obj.pos.r, c: obj.pos.c - 1},     // left
            {r: obj.pos.r, c: obj.pos.c + 1},     // right
            {r: obj.pos.r - 1, c: obj.pos.c - 1}, // up-left
            {r: obj.pos.r - 1, c: obj.pos.c + 1}, // up-right
            {r: obj.pos.r + 1, c: obj.pos.c - 1}, // down-left
            {r: obj.pos.r + 1, c: obj.pos.c + 1}  // down-right
        ];
        
        allMoves.forEach((o) => {
            let index = numCols * o.r + o.c;
            // Check bounds and letter match
            if (o.r >= 0 && o.r < numRows && o.c >= 0 && o.c < numCols) {
                if (board[o.r][o.c] === guess[obj.nextIndex] && !obj.path.includes(index)) {
                    let cloneObj = JSON.parse(JSON.stringify(obj));
                    cloneObj.pos = { r: o.r, c: o.c };
                    cloneObj.nextIndex += 1;
                    cloneObj.path.push(index);
                    queue.push(cloneObj);
                }
            }
        });
    };
    
    // BFS to find complete word
    while (queue.length > 0) {
        let obj = queue.shift();
        if (obj.nextIndex === guess.length) {
            return true;
        }
        exploreWord(obj, queue);
    }
    
    return false;
};

// Test with different words
console.log("BINGO:", checkWord(board, "BINGO"));
console.log("LINGO:", checkWord(board, "LINGO"));
console.log("BUNGIE:", checkWord(board, "BUNGIE"));
BINGO: true
LINGO: true
BUNGIE: false

How It Works

The algorithm maintains a queue of possible paths. Each queue item contains:

  • pos: Current position (row, column)

  • nextIndex: Index of next letter to find

  • path: Array of used cell indices to prevent reuse

For each position, it explores all 8 adjacent directions. If an adjacent cell contains the next required letter and hasn't been used in the current path, it creates a new path object and adds it to the queue.

Key Points

  • Uses BFS to explore all possible paths simultaneously

  • Converts 2D coordinates to 1D indices for efficient path tracking

  • Deep clones path objects to maintain separate exploration branches

  • Returns true as soon as any complete word path is found

Conclusion

This Boggle word validator uses breadth-first search to efficiently check if a word can be formed by connecting adjacent cells. The algorithm handles all 8 directions and prevents cell reuse within each path.

Updated on: 2026-03-15T23:19:00+05:30

574 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements