Given a matrix of lower alphabets and a dictionary. Find all words in the dictionary that can be found in the matrix. A word can start from any position in the matrix and go left/right/up/down to the adjacent position.

Example

Given matrix:

doaf
agai
dcan

and dictionary:

{"dog", "dad", "dgdg", "can", "again"}
 
return {"dog", "dad", "can", "again"}
 
Analysis:
DFS. For every word in the list, check every position of board, if the char at some position matches the first char in the word, then start a DFS at this position.
 
Solution:
 public class Solution {
/**
* @param board: A list of lists of character
* @param words: A list of string
* @return: A list of string
*/
public ArrayList<String> wordSearchII(char[][] board, ArrayList<String> words) {
ArrayList<String> res = new ArrayList<String>();
if (board.length==0) return res;
int rowNum = board.length;
if (board[0].length==0) return res;
int colNum = board[0].length; for (int i=0;i<words.size();i++){
String word = words.get(i);
if (word.length()==0) continue;
for (int j=0;j<rowNum;j++){
boolean valid = false;
for (int k=0;k<colNum;k++)
if (board[j][k]==word.charAt(0)){
boolean[][] visited = new boolean[rowNum][colNum];
for (int p = 0;p<rowNum;p++)
Arrays.fill(visited[p],false);
valid = isValidWord(board,visited,word,0,j,k);
if (valid){
res.add(word);
break;
}
}
if (valid) break;
}
} return res;
} public boolean isValidWord(char[][] board, boolean[][] visited, String word, int pos, int x, int y){
if (x<0 || x>=board.length || y<0 || y>=board[0].length) return false;
if (word.charAt(pos)!=board[x][y] || visited[x][y]) return false; if (pos==word.length()-1)
return true; visited[x][y] = true;
if (isValidWord(board,visited,word,pos+1,x+1,y) || isValidWord(board,visited,word,pos+1,x-1,y) || isValidWord(board,visited,word,pos+1,x,y+1) || isValidWord(board,visited,word,pos+1,x,y-1))
return true;
else {
visited[x][y]=false;
return false;
}
}
}

LintCode-Word Search II的更多相关文章

  1. Leetcode之回溯法专题-212. 单词搜索 II(Word Search II)

    Leetcode之回溯法专题-212. 单词搜索 II(Word Search II) 给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词. 单 ...

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

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

  3. 【leetcode】212. Word Search II

    Given an m x n board of characters and a list of strings words, return all words on the board. Each ...

  4. [LeetCode] Word Search II 词语搜索之二

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  5. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  6. 212. Word Search II

    题目: Given a 2D board and a list of words from the dictionary, find all words in the board. Each word ...

  7. Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  8. [LeetCode] 212. Word Search II 词语搜索之二

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  9. [LeetCode] 212. Word Search II 词语搜索 II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

  10. LeetCode() Word Search II

    超时,用了tire也不行,需要再改. class Solution { class TrieNode { public: // Initialize your data structure here. ...

随机推荐

  1. PHP浮点数的精度

    在百度知道上看到这么一个问题 var_dump((0.3-0.2)==0.1); 结果是:false 后来查查手册,原来是浮点数的精度问题.那么0.3-0.2-0.1等于多少呢,结果:2.775557 ...

  2. JavaScript学习笔记 - 进阶篇(1)- JS基础语法

    前言 JavaScript能做什么? 1.增强页面动态效果(如:下拉菜单.图片轮播.信息滚动等) 2.实现页面与用户之间的实时.动态交互(如:用户注册.登陆验证等) JS进阶篇学习什么? 在JavaS ...

  3. 关于C# webform 项目发布 aspx页面无任何代码 每个页面都以dll形式发布

    关于C# webform 项目发布 注意:aspx页面无任何代码,每个页面都以dll形式发布带bin文件夹里 具体操作方法如下图:

  4. php_2

    form表单提交: <body> <form action="php_request2.php" method="post"> 姓名: ...

  5. angularjs中的directive

    正在初学angularjs中,在网上看到一篇详细讲解directive指令的文章,于是就记录在这里和大家一起分享 angular.module('docsTransclusionExample', [ ...

  6. EF调用存储过程

    var objPar1 = new System.Data.Objects.ObjectParameter("ExeResults",typeof(bool)); var objP ...

  7. Python深拷贝和浅拷贝

    1- Python引用计数[1] 1.1 引用计数机制 引用计数是计算机编程语言中的一种内存管理技术,是指将资源(可以是对象.内存或磁盘空间等等)的被引用次数保存起来,当被引用次数变为零时就将其释放的 ...

  8. 【风马一族_Android】强制activity的横屏与纵屏

    <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="ht ...

  9. JDK 与 JRE (转)

    很多程序员已经干了一段时间java了依然不明白jdk与jre的区别.JDK就是Java Development Kit.简单的说JDK是面向开发人员使用的SDK,它提供了Java的开发环境和运行环境. ...

  10. Ubuntu14.04 安装 PHP cURL

    今天遇到 Fatal error: Call to undefined function curl_init() in /xxx/xxxx/www/application/library/Ku/Htt ...