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. asp.net MVC 如何隐藏 Response Header 版本号

    根据借楼最少资源原则,有时候MVC需要隐藏自己的版本号,其实这里也是比较简单的,只需要在Global.ascx 的Application_Start()中添加一行代码既可 添加为  MVCHandle ...

  2. 【CSS3】---曲线阴影翘边阴影

    效果图 代码 index <!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <title& ...

  3. 深入浅出ExtJS 第二章 Ext框架基础

    2.1 面向对象的基础架构(对象模型) 2.1.1 创建类 >.定义一个类: Ext.define('demo.Demo',{ name:'Lingo', hello:function () { ...

  4. LINQ简介和LINQ to SQL语句之Where

    LINQ是Language Integrated Query的简称,它是集成在.NET编程语言中的一种特性.已成为编程语言的一个组成部分,在编写程序时可以得到很好的编译时语法检查,丰富的元数据,智能感 ...

  5. 布料解算插件 Qualoth 重点参数分享

    前言 Qualoth是韩国FXGear公司推出的一款布料模拟插件,可以计算出很自然的衣褶以及动态效果,并且能应对大幅度动作的碰撞解算,可以和Houdini的Cloth Solver相媲美: 目前这款插 ...

  6. Incorporating ASP.NET MVC and SQL Server Reporting Services, Part 1

    Your ASP.NET MVC application needs reports. What do you do? In this article, I will demonstrate how ...

  7. wamp图标黄色的另一种可能原因

    学习php用的是wamp即windows+apache+mysql+php,但是当启动的时候,图标为黄色(正常应为绿色),开始以为是端口的问题,但是将端口改掉依旧如此,后来发现是机器装的sqlserv ...

  8. Newtonsoft.Json 基本用法

    Newtonsoft.Json 是.net 开源的一个json格式处理类库 官方网站:http://json.codeplex.com/ 在使用的项目引用Newtonsoft.Json库.平常使用的方 ...

  9. 将DataTable格式化为json字符串返回

    一般用于ajax局部刷新的使用比较多,通过查询得到了DataTable数据,要想将数据放回需要将DataTable转换为json格式,以下为转换的调用函数: string json = "& ...

  10. 8个3D视觉效果的HTML5动画欣赏

    现在的网页中应用了越来越多的3D应用,特别是基于HTML5 Canvas的动画特效,让用户有一种非常震撼的视觉体验.本文收集了8个非常炫酷的3D视觉效果的HTML5动画,都有源代码分享,你可以学习你感 ...