Diwali Offer!Flat 20% OFF
Enroll Now
PrakalpanaLive online tech training

105. Word Search

MediumDay 31 of 60Recursion & Backtracking

Given an m x n grid of letters board and a string word, return true if word can be traced in the grid. You trace a word by starting at any cell and moving one step up, down, left or right for each next letter. The same cell cannot be used twice in one word.

Example 1
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
Output: true
Example 2
Input: board = [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCB"
Output: false
The second B would need the first B again, which is not allowed.
Constraints
  • 1 <= m, n <= 6
  • 1 <= word.length <= 15
  • board and word contain only English letters
Hints
Frequently asked at: Amazon, Microsoft, Meta, Bloomberg
Ctrl + Enter runs the examples

Not sure what to learn next?

A 1-on-1 session with an industry mentor and a roadmap built for you. How it works

  1. 1Discuss your goals
  2. 2Get a personal plan
  3. 3Start learning
Book for β‚Ή200/ 30 min