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. 最长特殊序列 II

    最长特殊序列 II class Solution { boolean containsSub(String s,String p){ int i,j; for(i=0,j=0;i<p.lengt ...

  2. 拉格朗日乘子(Lagrange multify)和KKT条件

    拉格朗日乘子(Lagrange multify)和KKT条件 无约束问题 无约束问题定义如下: f(x)称为目标函数, 其中x是一个向量,它的维度是任意的. 通过求导, 令导数等于零即可: 如下图所示 ...

  3. 微信公众号开发调用摄像头、拍摄或选择图片、OCR识别

     一 .准备工作       <1> 域名认证准备工作 在需要调用摄像头的接口页面引入微信的js,具体地址为:(支持https):http://res.wx.qq.com/open/js/ ...

  4. 今天 运营同事发现的bug记录 上传商品时商品名称带双引号 导致输出页面时 双引号被转义

    例如  ”sk||““美白”淡化 这样输出表单页面时显示出来的只有sk||  解决办法 把输出文字对双引号进行转义

  5. python编程:从入门到实践----第六章:字典>练习

    6-1 人:使用一个字典来存储一个熟人的信息,包括名.姓.年龄和居住的城市.该字典应包含键first_name .last_name .age 和city .将存储在该字典中的每项信息都打印出来. f ...

  6. Tensorflow学习教程------下载图像识别模型inceptionV3

    # coding: utf-8 import tensorflow as tf import os import tarfile import requests #inception模型下载地址 in ...

  7. C盘满了解决办法之查看文件夹占用的统计界面

    TreeSize Free软件,可以实时查看文件夹使用情况: 百度搜索: 下载安装完以后的显示界面如下:

  8. iTOP-3399开发板搭建Android编译坏境

    基于迅为iTOP-3399开发板2.1 装 安装 d android  源码依赖包登录进 Ubuntu 系统,输入“ctrl+alt+t”,打开超级终端,使用“su root”命令,切换到 root ...

  9. 你知道你对 JSON Web Token 的认识存在误解吗

    1.前言 JSON Web Token (JWT) 其实目前已经广为软件开发者所熟知了,但是 JOSE (Javascript Object Signing and Encryption) 却鲜有人知 ...

  10. JavaScript可以做嵌入式开发了

    JS怎么可能搞嵌入式开发? TESSEL这个工具可直接在设备上运行JS,无需服务端支持. 百闻不如一见,请查看官网介绍吧:http://technical.io/ 网页内还有一个示例,点击会看到LED ...