Advertisement

Word Search - LeetCode 79 Solution

Word Search - Complete Solution Guide

Word Search is LeetCode problem 79, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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: 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 = "SEE"

Detailed Explanation

The problem asks us to determine if a given word can be found within a 2D grid (board) of characters. The word must be formed by sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. A cell can only be used once within the formation of the word.

Solution Approach

The solution uses a backtracking (Depth-First Search - DFS) approach to explore all possible paths in the grid. The main function `exist` iterates through each cell in the grid and calls a helper function `backtrack` to check if the word can be formed starting from that cell. The `backtrack` function recursively explores adjacent cells, checking if their characters match the next character in the word. To avoid revisiting the same cell, the current cell's character is temporarily replaced with a special character ('#'). After the exploration, the original character is restored to allow other paths to be explored correctly.

Step-by-Step Algorithm

  1. Step 1: Iterate through each cell (r, c) of the board.
  2. Step 2: For each cell, call the backtrack function with the starting position (r, c) and index 0 (start of the word).
  3. Step 3: The backtrack function checks the base cases: If the index equals the word's length, it means the entire word has been found, so return true. If the current position (r, c) is out of bounds or the character at the board[r][c] does not match the character at word[index], return false.
  4. Step 4: If the base cases are not met, temporarily mark the current cell as visited by replacing its character with '#'.
  5. Step 5: Recursively call the backtrack function for all four adjacent cells (up, down, left, right) with the index incremented by 1.
  6. Step 6: If any of the recursive calls return true (meaning the word was found along that path), return true.
  7. Step 7: Before returning, restore the original character of the current cell to the board.
  8. Step 8: If the backtrack function returns false for all starting cells, it means the word was not found in the board, so return false.

Key Insights

  • Insight 1: Backtracking is suitable because we need to explore all possible paths from each cell in the grid to see if any path matches the given word. If a path doesn't work, we need to backtrack and try a different path.
  • Insight 2: A key optimization is to temporarily mark visited cells during the backtracking search to avoid cycles and using the same cell multiple times.
  • Insight 3: The search process needs to start from every cell in the grid because the word can potentially start at any location.

Complexity Analysis

Time Complexity: O(M * N * 4^L)

Space Complexity: O(L)

Topics

This problem involves: Array, String, Backtracking, Depth-First Search, Matrix.

Companies

Asked at: Adobe, Amazon, Apple, Arista Networks, Atlassian, Bloomberg, Capital One, Cisco, Citadel, Epic Systems, Faire, FreshWorks, Goldman Sachs, Grammarly, Karat, MakeMyTrip, Meta, Microsoft, Morgan Stanley, Netflix, Oracle, PayPal, Paytm, Samsung, Snap, TikTok, Uber, Visa, Walmart Labs, Whatnot, Wix, Zepto, Zoho, tcs.