Word Search

Recursion FAQs (Hard) Hard
  • The underlying concept of this problem is often used in word puzzle games like "Boggle" or "Words With Friends", where players are required to search for words in grids
  • Specifically, the logic of checking for word existence in a grid by looking horizontally or vertically - without reusing the same letter cell, is an integral part of such game development to validate a player's move

Given a grid of n x m dimension grid of characters board and a string word.The word can be created by assembling the letters of successively surrounding cells, whether they are next to each other vertically or horizontally. It is forbidden to use the same letter cell more than once.

Return true if the word exists in the grid otherwise false.

Examples:

Input : board = [ ["A", "B", "C", "E"] , ["S" ,"F" ,"C" ,"S"] , ["A", "D", "E", "E"] ] , word = "ABCCED"

Output : true

Explanation : The word is coloured in yellow.


Input : board = [["A", "B", "C", "E"] , ["S", "F", "C", "S"] , ["A", "D", "E", "E"]] , word = "SEE"

Output : true

Explanation : The word is coloured in yellow.


Input : board = [["A", "B", "C", "E"] , ["S", "F", "C", "S"] , ["A", "D", "E", "E"]] , word = "ABCB"

Constraints

  • n = board.length
  • m = board[i].length
  • 1 <= n, m <=6
  • 1 <= word.length <= 15
  • board and word consist of only lowercase and uppercase English letters.

Hints

  • Start at every cell in the grid and try to match the first character of word. If a match is found, recursively explore the neighboring cells to match the subsequent characters. Mark the current cell as visited (e.g., by modifying its value temporarily) to prevent revisiting it in the same path.
  • If the current index of the word equals its length, it means the word is successfully found. If the current cell does not match the expected character or goes out of bounds, backtrack immediately.

Company Tags

Oracle Walmart Roche JPMorgan Chase Morgan Stanley Goldman Sachs Johnson & Johnson Activision Blizzard Shopify Ernst & Young OYO Rooms Intel Teladoc Health MongoDB Epic Systems Medtronic DoorDash Western Digital Rakuten Swiggy Riot Games Salesforce McKinsey & Company IBM Zoho Google Microsoft Amazon Meta Apple Netflix Adobe