DIY: Word Search I

Solve the interview question "Word Search I" in this lesson.

Problem statement

For the first challenge, you are given a n×nn \times n 2D grid of characters. You have to find a specific string in the grid by combining the adjacent characters. Assume that only up, down, right, and left neighbors are considered adjacent.

Input

The first input will be a two-dimensional list, meaning a list of lists. The 2D list will represent the grid of characters. The second input will be a string that needs to be searched in the grid. The following is an example input:

{{'H', 'O', 'L', 'I', 'K'}, 
{'O', 'M', 'L', 'M', 'E'}, 
{'O', 'E', 'I', 'A', 'Y'}, 
{'R', 'T', 'A', 'S', 'O'}, 
{'S', 'I', 'T', 'T', 'R'}}

"MAYOR"

Output

The output will be a Boolean that represents if the string was found in the grid or not. The following is an example output for the inputs above:

true

Coding exercise

You need to implement the function findString(grid, string), where grid is the 2D array of characters and string is the string we are searching for. The function returns a Boolean representing if the string was found.

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.