79. Word Search Leetcode Solution

Word Search Leetcode Solution:

Difficulty: Medium

Topics: Array, String, Backtracking, Matrix

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.

Example 1:

Word Search solution

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

Example 2:

Word Search Leetcode

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

Example 3:

Word Search

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

Constraints:

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

Follow-up: Could you use search pruning to make your solution faster with a larger board?

Solution:

To solve this problem, we can follow these steps:

Let’s implement this solution in PHP: 79. Word Search

Explanation:

  1. Initialization:
    • The exist function iterates through each cell in the grid. It starts a DFS search from each cell to see if the word can be constructed from that starting point.
  2. DFS Search (dfs function):
    • Bounds Check: The DFS function first checks if the current position is out of bounds or if the character at the current position doesn’t match the current character in the word.
    • Word Completion: If the current character matches and the end of the word is reached, it returns true.
    • Marking Cells: It marks the current cell as visited by setting it to '*' to avoid reusing the same cell.
    • Recursive Exploration: It recursively searches in the four possible directions (up, down, left, right).
    • Unmarking Cells: After exploring all directions, it restores the original character in the board.

This approach ensures that each cell is visited at most once per search and uses backtracking to ensure the solution remains efficient.
Contact Links

If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:

 

Read More

If you need any support regarding your website

If you like our blog, Please support us to improve

Buy Me a Coffee

Leave a Reply

Your email address will not be published. Required fields are marked *

RECENT POST
Leetcode Solutions

633. Sum of Square Numbers

Sum of Square Numbers Difficulty: Medium Topics: Math, Two Pointers, Binary Search Given a non-negative integer c, decide whether there’re

Leetcode Solutions

624. Maximum Distance in Arrays

Maximum Distance in Arrays Difficulty: Medium Topics: Array, Greedy You are given m arrays, where each array is sorted in