# Check if a given string can be formed using characters of adjacent cells of a Matrix

Given a matrix board of characters and a string Word, the task is to check if Word exists on the board constructed from a sequence of horizontally and vertically adjacent characters only. Each character can be used only once.

Examples:

Input:
board = { {‘A’, ‘B’, ‘C’, ‘E’}, {‘S’, ‘F’, ‘C’, ‘S’}, {‘A’, ‘D’, ‘E’, ‘E’} }
Word = “SEE”
Output: True
Explanation: “SEE” can be formed using characters at (1, 3)[S], (2, 3)[E] and (2, 2)[E].

Input:
board = { {‘A’, ‘B’, ‘C’, ‘E’}, {‘S’, ‘F’, ‘C’, ‘S’}, {‘A’, ‘D’, ‘E’, ‘E’} }
Word = “ABCB”
Output: False
Explanation: “ABCB” can not be formed by using adjacent characters without repetition.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The approach to solving this problem is to traverse all the characters in the matrix and find the occurrence of the first character of the word. Whenever found, recursively keep checking its adjacent horizontal and vertical cells for the next character. Repeat this process until all the characters are found one by one. Any instance where all the characters match signifies Word is found. If no such instance occurs, Word is not found.

Below is the implementation of the above logic:

 `// C++ Program to check if a given ` `// word can be formed from the ` `// adjacent characters in a matrix ` `// of characters ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to check if the word exists ` `bool` `checkWord(vector >& board, ` `               ``string& word, ``int` `index, ` `               ``int` `row, ``int` `col) ` `{ ` `    ``// If index exceeds board range ` `    ``if` `(row < 0 || col < 0 ` `        ``|| row >= board.size() ` `        ``|| col >= board[0].size()) ` `        ``return` `false``; ` ` `  `    ``// If the current cell does not ` `    ``// contain the required character ` `    ``if` `(board[row][col] != word[index]) ` `        ``return` `false``; ` ` `  `    ``// If the cell contains the required ` `    ``// character and is the last character ` `    ``// of the word required to be matched ` `    ``else` `if` `(index == word.size() - 1) ` ` `  `        ``// Return true as word is found ` `        ``return` `true``; ` ` `  `    ``char` `temp = board[row][col]; ` ` `  `    ``// Mark cell visited ` `    ``board[row][col] = ``'*'``; ` ` `  `    ``// Check Adjacent cells ` `    ``// for the next character ` `    ``if` `(checkWord(board, word, ` `                  ``index + 1, row + 1, col) ` `        ``|| checkWord(board, word, ` `                     ``index + 1, row - 1, col) ` `        ``|| checkWord(board, word, ` `                     ``index + 1, row, col + 1) ` `        ``|| checkWord(board, word, ` `                     ``index + 1, row, col - 1)) { ` ` `  `        ``board[row][col] = temp; ` ` `  `        ``return` `true``; ` `    ``} ` ` `  `    ``// Restore cell value ` `    ``board[row][col] = temp; ` `    ``return` `false``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``vector > board ` `        ``= { { ``'A'``, ``'B'``, ``'C'``, ``'E'` `}, ` `            ``{ ``'S'``, ``'F'``, ``'C'``, ``'S'` `}, ` `            ``{ ``'A'``, ``'D'``, ``'E'``, ``'E'` `} }; ` `    ``string word = ``"CFDASABCESEE"``; ` ` `  `    ``for` `(``int` `i = 0; i < board.size(); i++) { ` `        ``for` `(``int` `j = 0; j < board[0].size(); j++) { ` ` `  `            ``if` `(board[i][j] == word[0] ` `                ``&& checkWord( ` `                       ``board, word, ` `                       ``0, i, j)) { ` ` `  `                ``cout << ``"True"` `<< ``'\n'``; ` `                ``return` `0; ` `            ``} ` `        ``} ` `    ``} ` `    ``cout << ``"False"` `<< ``'\n'``; ` `    ``return` `0; ` `} `

 `// Java Program to check if a given ` `// word can be formed from the ` `// adjacent characters in a matrix ` `// of characters ` `import` `java.util.*; ` `class` `GFG{ ` ` `  `// Function to check if the word exists ` `static` `boolean` `checkWord(``char` `[][]board, ` `                 ``String word, ``int` `index, ` `                       ``int` `row, ``int` `col) ` `{ ` `    ``// If index exceeds board range ` `    ``if` `(row < ``0` `|| col < ``0` `||  ` `        ``row >= board.length ||  ` `        ``col >= board[``0``].length) ` `        ``return` `false``; ` ` `  `    ``// If the current cell does not ` `    ``// contain the required character ` `    ``if` `(board[row][col] != word.charAt(index)) ` `        ``return` `false``; ` ` `  `    ``// If the cell contains the required ` `    ``// character and is the last character ` `    ``// of the word required to be matched ` `    ``else` `if` `(index == word.length() - ``1``) ` ` `  `        ``// Return true as word is found ` `        ``return` `true``; ` ` `  `    ``char` `temp = board[row][col]; ` ` `  `    ``// Mark cell visited ` `    ``board[row][col] = ``'*'``; ` ` `  `    ``// Check Adjacent cells ` `    ``// for the next character ` `    ``if` `(checkWord(board, word, ` `                  ``index + ``1``, row + ``1``, col) ||  ` `        ``checkWord(board, word, ` `                  ``index + ``1``, row - ``1``, col) ||  ` `        ``checkWord(board, word, ` `                  ``index + ``1``, row, col + ``1``) ||  ` `        ``checkWord(board, word, ` `                  ``index + ``1``, row, col - ``1``))  ` `    ``{ ` `        ``board[row][col] = temp; ` ` `  `        ``return` `true``; ` `    ``} ` ` `  `    ``// Restore cell value ` `    ``board[row][col] = temp; ` `    ``return` `false``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``char``[][] board = { { ``'A'``, ``'B'``, ``'C'``, ``'E'` `}, ` `                       ``{ ``'S'``, ``'F'``, ``'C'``, ``'S'` `}, ` `                       ``{ ``'A'``, ``'D'``, ``'E'``, ``'E'` `} }; ` `    ``String word = ``"CFDASABCESEE"``; ` ` `  `    ``for` `(``int` `i = ``0``; i < board.length; i++)  ` `    ``{ ` `        ``for` `(``int` `j = ``0``; j < board[``0``].length; j++) ` `        ``{ ` `            ``if` `(board[i][j] == word.charAt(``0``) &&  ` `                ``checkWord(board, word, ``0``, i, j))  ` `            ``{ ` `                ``System.out.println(``"True"``); ` `                ``return``; ` `            ``} ` `        ``} ` `    ``} ` `    ``System.out.println(``"False"``); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

 `// C# program to check if a given word  ` `// can be formed from the adjacent  ` `// characters in a matrix of characters ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to check if the word exists ` `static` `bool` `checkWord(``char` `[,]board, String word,  ` `                      ``int` `index, ``int` `row, ``int` `col) ` `{ ` `     `  `    ``// If index exceeds board range ` `    ``if` `(row < 0 || col < 0 ||  ` `        ``row >= board.GetLength(0) ||  ` `        ``col >= board.GetLength(1)) ` `        ``return` `false``; ` ` `  `    ``// If the current cell does not ` `    ``// contain the required character ` `    ``if` `(board[row, col] != word[index]) ` `        ``return` `false``; ` ` `  `    ``// If the cell contains the required ` `    ``// character and is the last character ` `    ``// of the word required to be matched ` `    ``else` `if` `(index == word.Length - 1) ` ` `  `        ``// Return true as word is found ` `        ``return` `true``; ` ` `  `    ``char` `temp = board[row, col]; ` ` `  `    ``// Mark cell visited ` `    ``board[row, col] = ``'*'``; ` ` `  `    ``// Check adjacent cells ` `    ``// for the next character ` `    ``if` `(checkWord(board, word, ` `                  ``index + 1, row + 1, col) ||  ` `        ``checkWord(board, word, ` `                  ``index + 1, row - 1, col) ||  ` `        ``checkWord(board, word, ` `                  ``index + 1, row, col + 1) ||  ` `        ``checkWord(board, word, ` `                  ``index + 1, row, col - 1))  ` `    ``{ ` `        ``board[row, col] = temp; ` ` `  `        ``return` `true``; ` `    ``} ` ` `  `    ``// Restore cell value ` `    ``board[row, col] = temp; ` `    ``return` `false``; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``char``[,] board = { { ``'A'``, ``'B'``, ``'C'``, ``'E'` `}, ` `                      ``{ ``'S'``, ``'F'``, ``'C'``, ``'S'` `}, ` `                      ``{ ``'A'``, ``'D'``, ``'E'``, ``'E'` `} }; ` `    ``String word = ``"CFDASABCESEE"``; ` ` `  `    ``for``(``int` `i = 0; i < board.GetLength(0); i++)  ` `    ``{ ` `       ``for``(``int` `j = 0; j < board.GetLength(1); j++) ` `       ``{ ` `          ``if` `(board[i, j] == word[0] &&  ` `              ``checkWord(board, word, 0, i, j))  ` `          ``{ ` `              ``Console.WriteLine(``"True"``); ` `              ``return``; ` `          ``} ` `       ``} ` `    ``} ` `    ``Console.WriteLine(``"False"``); ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output:
```True
```

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : Rajput-Ji

Article Tags :
Practice Tags :