Given a 2D board and a word, find if the word exists in the grid.

The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.

Example:

board =
[
['A','B','C','E'],
['S','F','C','S'],
['A','D','E','E']
] Given word = "ABCCED", return true.
Given word = "SEE", return true.
Given word = "ABCB", return false. Time: O(M * N * 4^|Word|)
 class Solution {
int row;
int col;
public boolean exist(char[][] board, String word) {
row = board.length;
col = board[0].length;
boolean[][] visited = new boolean[row][col];
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (helper(board, visited, word, i, j, 0)) {
return true;
}
}
}
return false;
} private boolean helper(char[][] board, boolean[][] visited, String word, int i, int j, int index) {
if (index == word.length()) {
return true;
}
if (i < 0 || i >= row || j < 0 || j >= col) {
return false;
}
if (word.charAt(index) == board[i][j] && !visited[i][j]) {
visited[i][j] = true;
boolean res = helper(board, visited, word, i + 1, j, index + 1) ||
helper(board, visited, word, i - 1, j, index + 1) ||
helper(board, visited, word, i, j + 1, index + 1) ||
helper(board, visited, word, i, j - 1, index + 1);
// need to clean up visited
visited[i][j] = false;
return res;
} return false;
}
}
class Solution {
int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
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 (dfs(0, word, board, visited, i, j)) {
return true;
}
}
}
return false;
} private boolean dfs(int index, String word, char[][] board, boolean[][] visited, int row, int col) {
if (index == word.length()) {
return true;
}
// need to check edge after base case e.g. [['A']] 'A', is actually out of board
if (row < 0 || row >= board.length || col < 0 || col >= board[0].length || visited[row][col]) {
return false;
}
if (board[row][col] == word.charAt(index)) {
visited[row][col] = true;
for (int[] direction: directions) {
int nxtRow = direction[0] + row;
int nxtCol = direction[1] + col;
if (dfs(index + 1, word, board, visited, nxtRow, nxtCol)) {
return true;
}
}
visited[row][col] = false;
}
return false;
}
}

[LC] 79. Word Search的更多相关文章

  1. 刷题79. Word Search

    一.题目说明 题目79. Word Search,给定一个由字符组成的矩阵,从矩阵中查找一个字符串是否存在.可以连续横.纵找.不能重复使用,难度是Medium. 二.我的解答 惭愧,我写了很久总是有问 ...

  2. [LeetCode] 79. Word Search 词语搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  3. 【LeetCode】79. Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

  4. [LeetCode] 79. Word Search 单词搜索

    Given a 2D board and a word, find if the word exists in the grid. The word can be constructed from l ...

  5. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  6. LeetCode OJ 79. Word Search

    题目 Given a 2D board and a word, find if the word exists in the grid. The word can be constructed fro ...

  7. 79. Word Search在字母矩阵中查找单词

    [抄题]: Given a 2D board and a word, find if the word exists in the grid. The word can be constructed ...

  8. 79. Word Search

    使用的别人的思路,用一个二维数组记录每一个位置是否用过,然后通过递归来判断每一个位置是否符合 public class Solution { public boolean exist(char[][] ...

  9. Leetcode#79 Word Search

    原题地址 依次枚举起始点,DFS+回溯 代码: bool dfs(vector<vector<char> > &board, int r, int c, string ...

随机推荐

  1. 2020/1/28 PHP代码审计之代码执行漏洞

    0x00代码执行原理 应用程序在调用一些能够将字符串转换为代码的函数(如PHP中的eval)时,没有考虑用户是否控制这个字符串,将造成代码执行漏洞. 该漏洞主要存在于eval().assert().p ...

  2. HDU 2444 The Accomodation of Students【二分图最大匹配问题】

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2444 题意:首先判断所有的人可不可以分成互不认识的两部分.如果可以分成 ,则求两部分最多相互认识的对数. ...

  3. 【MySQL优化】数据库结构优化

    原则: 设计表结构,字段类型,最小化磁盘存储的空间,减少IO.数据库操作中最为耗时的操作就是 IO 处理,大部分数据库操作 90% 以上的时间都花在了 IO 读写上面.所以尽可能减少 IO 读写量,可 ...

  4. 吴裕雄--天生自然 PHP开发学习:表单 - 验证邮件和URL

    $name = test_input($_POST["name"]); if (!preg_match("/^[a-zA-Z ]*$/",$name)) { $ ...

  5. 第4章 ZK基本特性与基于Linux的ZK客户端命令行学习

    第4章 ZK基本特性与基于Linux的ZK客户端命令行学习 4-1 zookeeper常用命令行操作 4-2 session的基本原理与create命令的使用

  6. PAT Advanced 1088 Rational Arithmetic (20) [数学问题-分数的四则运算]

    题目 For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate ...

  7. PAT Advanced 1049 Counting Ones (30) [数学问题-简单数学问题]

    题目 The task is simple: given any positive integer N, you are supposed to count the total number of 1 ...

  8. day63-html-列表,表格,标签的嵌套规则

    1.列表 1.无序列表 <ul type="disc"> <li>a</li> <li>b</li> </ul&g ...

  9. gitKraken取消/关闭全屏

    如果你找不到在哪里设置的 这是配置文件  注意 fullScreen 字段,改这个字段可以改变是不是全屏,改变之前先关闭软件, 文件目录 第二张图

  10. ZJNU 1069 - 表达式的转换——中级

    栈运用的模板题,对于符号进行出入栈操作,每次与栈顶的符号进行优先级判断,得出第一行后缀表达式. 在其后的化简计算中,每次用一个特殊符号(代码中使用了'?')代替原来的计算结果引用,并开一个数组表示每次 ...