Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Word Search Board Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Word Search Board

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

You are given a board of size n*m, containing an English alphabet in each cell. You are also given a word, find if the word exists on the board. The word can be constructed from letters of sequentially adjacent cells. Cells which have a common edge are considered adjacent. The same cell may not be used more than once.

Approach

We can model this into a DFS on-grid problem. We start from the 1st character of the word to find as root and recursively traverse to adjacent valid cells to find the next character of the word. When we find the whole word, we return true, else after checking all possible cases, we return false.

Analysis

  • Time Complexity: O(n * m)
  • Space Complexity: O(1)

Implementation

C++
bool wordSearch(vector<vector<char>>& board, string word, int i, int j, int index) {
	if(index == word.size()) {
		return true;
	}
	int n = board.size();
	int m = board[0].size();
	if(i < 0 || j < 0 || i >= n || j >= m) {
		return false;
	}
	if(board[i][j] != word[index]) {
		return false;
	}
	bool returnValue = wordSearch(board, word, i + 1, j, index + 1);
	returnValue |= wordSearch(board, word, i - 1, j, index + 1);
	returnValue |= wordSearch(board, word, i, j + 1, index + 1);
	returnValue |= wordSearch(board, word, i, j - 1, index + 1);
	return returnValue;
}
bool wordExists(vector<vector<char>> board, string word) {
    int wordLen = word.size();
	if(wordLen == 0) {
		return true;
	}
	int n = board.size();
	int m = board[0].size();
	if(n == 0 || m == 0) {
		return 0;
	}
	for(int i = 0; i < n; i++) {
		for(int j = 0; j < m; j++) {
			if(board[i][j] == word[0]) {
				if(wordSearch(board, word, i, j, 0)) {
					return true;
				}
			}
		}
	}
	return false;
}
Java
class Solution {
	boolean wordSearch(char[][] board, String word, int i, int j, int index) {
		if(index == word.length()) {
			return true;
		}
		int n = board.length;
		int m = board[0].length;
		if(i < 0 || j < 0 || i >= n || j >= m) {
			return false;
		}
		if(board[i][j] != word.charAt(index)) {
			return false;
		}
		boolean returnValue = wordSearch(board, word, i + 1, j, index + 1);
		returnValue |= wordSearch(board, word, i - 1, j, index + 1);
		returnValue |= wordSearch(board, word, i, j + 1, index + 1);
		returnValue |= wordSearch(board, word, i, j - 1, index + 1);
		return returnValue;
	}
	boolean wordExists(char[][] board, String word) {
    	int wordLen = word.length();
		if(wordLen == 0) {
			return true;
		}
		int n = board.length;
		int m = board[0].length;
		if(n == 0 || m == 0) {
			return false;
		}
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < m; j++) {
				if((board[i][j] - 'A') == (word.charAt(0) - 'A')) {
					if(wordSearch(board, word, i, j, 0)) {
						return true;
					}
				}
			}
		}
		return false;
	}
}
Related Content
Adjacency List to Adjacency Matrix
Adjacency Matrix to Adjacency List
BFS of a Graph
Capture Surrounded Regions
Clone a Graph
DFS of an Acyclic Graph
DFS of a Cyclic Graph
Edges to Adjacency List
Flood Fill Image
Knight's Journey On A Chessboard
M-Coloring Problem
Number of Islands
Stepping Numbers
Valid Path
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet