All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0001_0100.s0079_word_search.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g0001_0100.s0079_word_search;

// #Medium #Top_100_Liked_Questions #Top_Interview_Questions #Array #Matrix #Backtracking
// #Algorithm_II_Day_11_Recursion_Backtracking #Big_O_Time_O(4^(m*n))_Space_O(m*n)
// #2023_08_11_Time_157_ms_(78.97%)_Space_40.5_MB_(84.41%)

/**
 * 79 - 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.
 *
 * **Example 1:**
 *
 * ![](https://assets.leetcode.com/uploads/2020/11/04/word2.jpg)
 *
 * **Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "ABCCED"
 *
 * **Output:** true 
 *
 * **Example 2:**
 *
 * ![](https://assets.leetcode.com/uploads/2020/11/04/word-1.jpg)
 *
 * **Input:** board = \[\["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]], word = "SEE"
 *
 * **Output:** true 
 *
 * **Example 3:**
 *
 * ![](https://assets.leetcode.com/uploads/2020/10/15/word3.jpg)
 *
 * **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`?
**/
public class Solution {
    private boolean backtrace(
            char[][] board, boolean[][] visited, String word, int index, int x, int y) {
        if (index == word.length()) {
            return true;
        }
        if (x < 0 || x >= board.length || y < 0 || y >= board[0].length || visited[x][y]) {
            return false;
        }
        visited[x][y] = true;
        if (word.charAt(index) == board[x][y]) {
            boolean res =
                    backtrace(board, visited, word, index + 1, x, y + 1)
                            || backtrace(board, visited, word, index + 1, x, y - 1)
                            || backtrace(board, visited, word, index + 1, x + 1, y)
                            || backtrace(board, visited, word, index + 1, x - 1, y);
            if (!res) {
                visited[x][y] = false;
            }
            return res;
        } else {
            visited[x][y] = false;
            return false;
        }
    }

    public boolean exist(char[][] board, String word) {
        boolean[][] visited = new boolean[board.length][board[0].length];
        for (int i = 0; i < board.length; ++i) {
            for (int j = 0; j < board[0].length; ++j) {
                if (backtrace(board, visited, word, 0, i, j)) {
                    return true;
                }
            }
        }
        return false;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy