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. GDB使用

    1.display val 设置显示格式 2.i b显示所有断点

  2. C#垃圾回收机制

    C#属于托管的面相对象的语言,内存回收机制就是一个代表, C#有一套类似"全自动"的垃圾回收机制,也就是虚拟机会自动来判断执行内存的回收, 我们一般常用的Dispose(),Usi ...

  3. Android中Json数据读取与创建

    一:  Json的特性和在数据交互中的地位就不用说了,直接看案例. 首先在android studio中创建assets文件目录,用于存放Json数据文件,android studio 1.3 默认项 ...

  4. 关于div 浮动在select,或table控件之上

    <div style="position:absolute; display:none; z-index:99999" id="d3" onmouseov ...

  5. 关于Java中计算日期差值不准确问题

    1.字符串日期相减 如:2016-4-1,必须先将此字符串转成Date对象,并且, 格式必须为:yyyy—MM—dd  HH:mm:ss. 如果不转就直接计算(2016-4-1)两个这样的日期,则误差 ...

  6. tar命令: 对某目录文件打tar包时,排除指定的目录或文件

     如某当前目录存在以下文件或目录: 1.txt2.txt3.txtdir1dir2my2015.tarmy2016.tar 若要对当前目录除1.txt 和dir1.tar外,打包tar 步骤一.建立e ...

  7. CSS样式表与格式布局

    样式表 CSS(Cascading Style Sheets  层叠样式表),作用是美化HTML网页. 内联样式表: 例:<p style="font-size:10px;" ...

  8. 1.linux概述及如何访问

    1.linux终端访问及退出 1.1访问 linux有7个终端:(1个真实终端+6个虚拟终端) ctril+alt+Fn (Fn是指F1\F2..)切换终端 1.2退出: shutdown 缓冲一会关 ...

  9. Win7中隐藏的上帝模式——GodMode

    Win7中隐藏的上帝模式——GodMode ~ Windows7中的隐藏模式 ~   随意新建一个文件夹吧,然后重命名为:   GodMode.{ED7BA470-8E54-465E-825C-997 ...

  10. C++ Strings(字符串)

    Constructors 构造函数,用于字符串初始化 Operators 操作符,用于字符串比较和赋值 append() 在字符串的末尾添加文本 assign() 为字符串赋新值 at() 按给定索引 ...