Word Search II

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=0):_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=0;i<insert_str_size;i++){
            if(cur->_children[insert_str[i]-‘a‘]==NULL){
                Node *node = new Node(insert_str[i],false,26);
                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<0 || cur_r>=row || cur_c<0 || 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-1,cur_c);
            dfs(board,visited,sets,str,child,row,col,cur_r,cur_c+1);
            dfs(board,visited,sets,str,child,row,col,cur_r+1,cur_c);
            dfs(board,visited,sets,str,child,row,col,cur_r,cur_c-1);
        }
        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,26));
        for(size_t i=0;i<words.size();i++){
            root.insert(words[i]);
        }
        int row = board.size();
        int col = board[0].size();
        vector<vector<bool>> visited(row,vector<bool>(col,false));
        string str;
        unordered_set<string> sets;
        for(size_t i=0;i<row;i++){
            for(size_t j=0;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;
    }
};
时间: 2024-10-13 09:58:14

Word Search II的相关文章

[LeetCode] Word Search II

A simple combination of Implement Trie (Prefix Tree) and Word Search. If you've solved them, this problem will become easy :-) The following code is based on DFS and should be self-explanatory enough. Well, just go ahead and read it. It is long but c

leetcode笔记:Word Search II

一. 题目描述 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

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 must be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same le

【LeetCode】Word Search II 解题报告

[题目] 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 sa

[leedcode 212] Word Search II

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 le

[LeetCode#212]Word Search II

Problem: 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. Th

Word Search II 460s

1 public class Solution { 2 public List<String> findWords(char[][] board, String[] words) { 3 Trie trie = new Trie(); 4 for (String word : words) { 5 trie.add(word); 6 } 7 8 List<String> res = new ArrayList<String>(); 9 for (int i = 0; i

[Swift]LeetCode212. 单词搜索 II | Word Search II

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 le

[leetcode trie]212. Word Search II

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 le