Word Search - Check if a word exists in a grid or not
Last Updated : 29 Sep, 2025
Given a 2D array mat[][] of characters and a string word, check whether the word exists in the array or not. The word can be matched in four possible directions: horizontally left or right, and vertically up or down. Each cell can be used only once in forming the word.
Examples:
Input: mat[][] = [['T', 'E', 'E'], ['S', 'G', 'K'], ['T', 'E', 'L']], word = "GEEK" Output: true Explanation: Word "GEEK" can be found in the given grid as follows
Input: mat[][] = [['T', 'E', 'U'], ['S', 'G', 'K'], ['T', 'E', 'L']], word = "GEEK" Output: false Explanation: Word "GEEK" cannot be found in the given grid.
The idea is to search for the word in the grid by exploring all possible paths. For every cell that matches the first character of the word, we recursively explore in four directions — up, down, left, and right — to check if the entire word can be formed. We mark cells as visited temporarily during the search to avoid revisiting the same cell in the current path, and backtrack after exploring all possibilities from that cell.
Working:
C++
#include<iostream>#include<vector>usingnamespacestd;// Recursive Function to check if the word exists in the matrix or notboolfindMatch(vector<vector<char>>&mat,string&word,intx,inty,intwIdx){intwLen=word.length();intn=mat.size();intm=mat[0].size();if(wIdx==wLen)returntrue;// Out of Boundaryif(x<0||y<0||x>=n||y>=m)returnfalse;// If grid matches with a letter while// recursionif(mat[x][y]==word[wIdx]){// Marking this cell as visitedchartemp=mat[x][y];mat[x][y]='#';// finding subpattern in 4 directionsboolres=findMatch(mat,word,x-1,y,wIdx+1)||findMatch(mat,word,x+1,y,wIdx+1)||findMatch(mat,word,x,y-1,wIdx+1)||findMatch(mat,word,x,y+1,wIdx+1);mat[x][y]=temp;returnres;}returnfalse;}// Function to check if the word exists in the matrix or notboolisWordExist(vector<vector<char>>&mat,string&word){intwLen=word.length();intn=mat.size();intm=mat[0].size();// if total characters in matrix is// less than word lengthif(wLen>n*m)returnfalse;for(inti=0;i<n;i++){for(intj=0;j<m;j++){// If first letter matches, then recur and checkif(mat[i][j]==word[0]){if(findMatch(mat,word,i,j,0))returntrue;}}}returnfalse;}intmain(){vector<vector<char>>mat={{'T','E','E'},{'S','G','K'},{'T','E','L'}};stringword="GEEK";cout<<(isWordExist(mat,word)?"true":"false");return0;}
Java
classGFG{// Recursive Function to check if the word exists in the matrix or notstaticbooleanfindMatch(char[][]mat,Stringword,intx,inty,intwIdx){intwLen=word.length();intn=mat.length;intm=mat[0].length;if(wIdx==wLen)returntrue;// Out of Boundaryif(x<0||y<0||x>=n||y>=m)returnfalse;// If grid matches with a letter while recursionif(mat[x][y]==word.charAt(wIdx)){// Marking this cell as visitedchartemp=mat[x][y];mat[x][y]='#';// finding subpattern in 4 directionsbooleanres=findMatch(mat,word,x-1,y,wIdx+1)||findMatch(mat,word,x+1,y,wIdx+1)||findMatch(mat,word,x,y-1,wIdx+1)||findMatch(mat,word,x,y+1,wIdx+1);mat[x][y]=temp;returnres;}returnfalse;}// Function to check if the word exists in the matrix or notstaticbooleanisWordExist(char[][]mat,Stringword){intwLen=word.length();intn=mat.length;intm=mat[0].length;// if total characters in matrix is less than word lengthif(wLen>n*m)returnfalse;for(inti=0;i<n;i++){for(intj=0;j<m;j++){// If first letter matches, then recur and checkif(mat[i][j]==word.charAt(0)){if(findMatch(mat,word,i,j,0))returntrue;}}}returnfalse;}publicstaticvoidmain(String[]args){char[][]mat={{'T','E','E'},{'S','G','K'},{'T','E','L'}};Stringword="GEEK";System.out.println(isWordExist(mat,word));}}
Python
# Recursive Function to check if the word exists in the matrix or notdeffindMatch(mat,word,x,y,wIdx):wLen=len(word)n=len(mat)m=len(mat[0])ifwIdx==wLen:returnTrue# Out of Boundaryifx<0ory<0orx>=nory>=m:returnFalse# If grid matches with a letter while# recursionifmat[x][y]==word[wIdx]:# Marking this cell as visitedtemp=mat[x][y]mat[x][y]='#'# finding subpattern in 4 directionsres=(findMatch(mat,word,x-1,y,wIdx+1)orfindMatch(mat,word,x+1,y,wIdx+1)orfindMatch(mat,word,x,y-1,wIdx+1)orfindMatch(mat,word,x,y+1,wIdx+1))mat[x][y]=tempreturnresreturnFalsedefisWordExist(mat,word):wLen=len(word)n=len(mat)m=len(mat[0])# if total characters in matrix is# less than word lengthifwLen>n*m:returnFalseforiinrange(n):forjinrange(m):# If first letter matches, then recur and checkifmat[i][j]==word[0]:iffindMatch(mat,word,i,j,0):returnTruereturnFalseif__name__=="__main__":mat=[['T','E','E'],['S','G','K'],['T','E','L']]word="GEEK"print("true"ifisWordExist(mat,word)else"false")
C#
usingSystem;classGFG{// Recursive Function to check if the word exists in the matrix or notstaticboolfindMatch(char[,]mat,stringword,intx,inty,intwIdx){intwLen=word.Length;intn=mat.GetLength(0);intm=mat.GetLength(1);if(wIdx==wLen)returntrue;// Out of Boundaryif(x<0||y<0||x>=n||y>=m)returnfalse;// If grid matches with a letter while// recursionif(mat[x,y]==word[wIdx]){// Marking this cell as visitedchartemp=mat[x,y];mat[x,y]='#';// finding subpattern in 4 directionsboolres=findMatch(mat,word,x-1,y,wIdx+1)||findMatch(mat,word,x+1,y,wIdx+1)||findMatch(mat,word,x,y-1,wIdx+1)||findMatch(mat,word,x,y+1,wIdx+1);mat[x,y]=temp;returnres;}returnfalse;}// Function to check if the word exists in the matrix or notstaticboolisWordExist(char[,]mat,stringword){intwLen=word.Length;intn=mat.GetLength(0);intm=mat.GetLength(1);// if total characters in matrix is// less than word lengthif(wLen>n*m)returnfalse;for(inti=0;i<n;i++){for(intj=0;j<m;j++){// If first letter matches, then recur and checkif(mat[i,j]==word[0]){if(findMatch(mat,word,i,j,0))returntrue;}}}returnfalse;}staticvoidMain(){char[,]mat={{'T','E','E'},{'S','G','K'},{'T','E','L'}};stringword="GEEK";Console.WriteLine(isWordExist(mat,word)?"true":"false");}}
JavaScript
// Recursive Function to check if the word exists in the matrix or notfunctionfindMatch(mat,word,x,y,wIdx){constwLen=word.length;constn=mat.length;constm=mat[0].length;if(wIdx===wLen)returntrue;// Out of Boundaryif(x<0||y<0||x>=n||y>=m)returnfalse;// If grid matches with a letter while// recursionif(mat[x][y]===word[wIdx]){// Marking this cell as visitedconsttemp=mat[x][y];mat[x][y]='#';// finding subpattern in 4 directionsconstres=findMatch(mat,word,x-1,y,wIdx+1)||findMatch(mat,word,x+1,y,wIdx+1)||findMatch(mat,word,x,y-1,wIdx+1)||findMatch(mat,word,x,y+1,wIdx+1);mat[x][y]=temp;returnres;}returnfalse;}// Function to check if the word exists in the matrix or notfunctionisWordExist(mat,word){constwLen=word.length;constn=mat.length;constm=mat[0].length;// if total characters in matrix is// less than word lengthif(wLen>n*m)returnfalse;for(leti=0;i<n;i++){for(letj=0;j<m;j++){// If first letter matches, then recur and checkif(mat[i][j]===word[0]){if(findMatch(mat,word,i,j,0))returntrue;}}}returnfalse;}// Driver Codeconstmat=[['T','E','E'],['S','G','K'],['T','E','L']];constword="GEEK";console.log(isWordExist(mat,word)?"true":"false");
Output
true
Time Complexity: O(n*m * 4k), where n × m is the matrix size and k is the word length Auxiliary Space: O(k), due to recursion stack depth