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

Each word must 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 in a word.

For example,
Given words = ["oath","pea","eat","rain"] and board =

[
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]

Return ["eat","oath"].

一.思路类比

在Word Search:http://www.cnblogs.com/zengzy/p/4941110.html中说过,一个单词word在board中搜索的时间复杂度为O(m*n*m*n),如果有k个word那么时间复杂度是o(k*m*n*m*n)。那么,是否好优化一点的方式呢?我们对比Word Search2和1的不同,会发现,WS2(Word Search2)就是有k个单词,而WS1只有一个单词,所以我们希望有一种结构可以把k个单词组织起来,使得我们在k个单词中查找某个单词就像在只在一个单词中查找一样,换句话说就是,我们希望在k个单词中查找某个单词的时间成本是O(word_length)。这种结构可以借用树+哈希来完成,单词的每个字母就是树中的一个结点,而下一个字母就是该结点的下一个结点,可以用哈希表存储,而这种结构也叫字典树或键树。

二.时间复杂度

上文已经描述过,虽然有k个单词,但其对外表现就像只有一个单词,因此时间复杂度还是O(m*n*m*n)。

三.空间复杂度

在该题中,结点的子结点可以使用一个大小为26的数组表示,即26叉树:

struct node{

char c;//结点值

bool isWord;该结点是否为某个单词的结尾

struct node* children[26];

}

树的高度为最长单词的长度,设为len,那么空间复杂度为o(26^0+26^2+...+26^len)

四.代码,注意此处代码中存在内存泄露的问题,因为new之后析构函数并没有释放掉内存。

class Tree;
class Solution;
class Node{
public:
friend class Tree;
friend class Solution;
private:
char _c;
bool _isword;
vector<Node*> _children;
public:
Node(char c,bool isword=false,size_t children_size=):_c(c),_isword(isword),_children(children_size,NULL){}
bool getIsWord()
{
return _isword;
}
}; class Tree{
private:
Node* _root;
public:
Tree(Node* root=NULL)
{
_root = root ;
}
void insert(const string& insert_str)
{
size_t insert_str_size = insert_str.size();
Node* cur = _root;
for(size_t i=;i<insert_str_size;i++){
if(cur->_children[insert_str[i]-'a']==NULL){
Node *node = new Node(insert_str[i],false,);
cur->_children[insert_str[i]-'a']=node;
cur = node;
}else{
cur = cur->_children[insert_str[i]-'a'];
}
}
cur->_isword = true;
}
Node* getRoot()
{
return _root;
}
}; class Solution {
public:
void dfs(vector<vector<char>>& board,vector<vector<bool>>& visited,unordered_set<string>& sets,string& str,Node* node,int row,int col,int cur_r,int cur_c)
{
if(cur_r< || cur_r>=row || cur_c< || cur_c>=col || visited[cur_r][cur_c]){
return ;
}
visited[cur_r][cur_c] = true;
char c = board[cur_r][cur_c] ;
str.push_back(c);
Node* child = node->_children[c-'a'];
if(child != NULL){
if(child->getIsWord())
sets.insert(str);
dfs(board,visited,sets,str,child,row,col,cur_r-,cur_c);
dfs(board,visited,sets,str,child,row,col,cur_r,cur_c+);
dfs(board,visited,sets,str,child,row,col,cur_r+,cur_c);
dfs(board,visited,sets,str,child,row,col,cur_r,cur_c-);
}
visited[cur_r][cur_c] = false;
str.pop_back();
} vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
vector<string> res;
if(board.empty()){
return res;
}
Tree root(new Node('#',false,));
for(size_t i=;i<words.size();i++){
root.insert(words[i]);
}
int row = board.size();
int col = board[].size();
vector<vector<bool>> visited(row,vector<bool>(col,false));
string str;
unordered_set<string> sets;
for(size_t i=;i<row;i++){
for(size_t j=;j<col;j++){
dfs(board,visited,sets,str,root.getRoot(),row,col,i,j);
}
}
for(auto iter = sets.begin();iter!=sets.end();iter++){
res.push_back(*iter);
}
return res;
}
};

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. [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 ...

  8. [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 ...

  9. LeetCode() Word Search II

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

随机推荐

  1. mysql 数据库连接池

    hibernate配置C3P0详解         分类:             hibernate              2013-08-14 16:16     1213人阅读     评论 ...

  2. 如何利用自己的电脑做服务器发布tomcat的WEB项目供外网访问

    1.首先你要确定你有一个外网ip地址.如果你分配到的是一个局域网IP地址需要经过一系列的转换为外网ip地址,然后继续下面操作. 2.拿到外网IP地址,进行tomcat的server.xml文件的配置. ...

  3. hdu 1587

    Problem Description As you know, Gardon trid hard for his love-letter, and now he's spending too muc ...

  4. 关于onpropertychange与oninput的兼容问题

    关于onpropertychange与oninput的用法,网上一大堆,但还是有不兼容的时候,比如说,我想计下,一个input的值改变了多少次,如果写成兼容写法就为 <!doctype html ...

  5. activiti笔记二:用户任务

    1, assignee 代替humanPerformer  功能 2, cadidateUsers代替potentialOwner功能 3, candidateGroups代替potentialOwn ...

  6. SqlServer排序(null值,和非空值排列顺序)

    项目中遇到一个问题,需要设置序号排序,而该字段中的默认值为空,使用普通排序,空值就会在最前边.可以使用如下语句:   其中 col 为 排序的字段名称. then 0 else 1 代表先空值,后数字 ...

  7. Git工作中用法(Gitlab)

    感觉又有了新的认识.  一共有3个仓库,本地自己的,远程自己的,远程主仓库. 为了方便能及时从主仓库获取更新的内容要将远程主仓库也clone下来 git clone upstream url    / ...

  8. centos6.4x64安装vncserver

    参考文章:http://blog.csdn.net/mchdba/article/details/43058849 主要是远程安装oracle11g需要用到这个东西: 进入系统依次执行下列命令: #查 ...

  9. mvc 解决StyleBundle中 图片绝对路径 装换成相对路径的问题 CssRewriteUrlTransform

    问题 解决办法

  10. 大量客户反映wordpress的网站打开巨慢,经分析发现,这些网站大都使用了google的字体服务,由于最近google的服务已经被大陆屏蔽,所以wordpress的网站打开时,会卡在字体加载上。

     一会你安装完wp,发现打开巨卡的话,看看这个帖子:http://bbs.myhostcn.com/thread-1026-1-1.html最近一段时间,大量客户反映wordpress的网站打开巨慢, ...