BackTracking : WordSearch :4April POTD
Day 9/75:
Question: Word Search -(Medium)
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
Input: board = [["o","a","a","n"],["e","t","a","e"],["i","h","k","r"],["i","f","l","v"]], words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]
Approach:
Approach:
Iterating through the Grid: We iterate through each cell in the grid using two nested loops.
Backtracking with DFS: We define a recursive function find to perform DFS from a specific cell (i, j) in the grid. In each recursive call, we perform the following steps:
Check if the current index idx equals the length of the word. If it does, it means we have found the entire word, so we return true.
领英推荐
Check if the current cell (i, j) is out of bounds or has been visited before. If it is, or if the character in the current cell does not match the character at index idx in the word, we return false.
If the character in the current cell matches the character at index idx, we mark the cell as visited (to avoid revisiting) and recursively explore its neighboring cells.
If any of the recursive calls return true, indicating that the word is found starting from the current cell, we propagate the true value back up the call stack.
If none of the recursive calls return true, we backtrack by marking the current cell as unvisited and continue exploring other paths.
Code:
Complexity Analysis:
WORD SEARCH II : https://leetcode.com/problems/word-search-ii/description/
?? Congratulations, Suyash Awathe for engineering feats of coding marvel that defy gravity! Your recent achievement is a testament to your ability to bend the rules of logic and harness the power of innovation. Like a master architect designing structures that reach for the sky, you construct solutions that elevate the coding landscape to new heights. Keep building the skyscrapers of success! ????