Leetcode 212. Word Search II

Posted haoweizh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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. The same letter cell may not be used more than once in a word.

Example:

Input: 
words = ["oath","pea","eat","rain"] and board =
[
  [‘o‘,‘a‘,‘a‘,‘n‘],
  [‘e‘,t‘,‘a‘,‘e‘],
  [‘i‘,h‘,‘k‘,‘r‘],
  [‘i‘,‘f‘,‘l‘,‘v‘]
]

Output: ["eat","oath"]

Note:
You may assume that all inputs are consist of lowercase letters a-z.

Solution:

  这道题要求我们在board中找出所有在words中出现过的单词。和Leetcode 79一样一个单词一个单词判断效率是非常低的。因此,我们可以先对所有单词建立一个字典树,然后用dfs在board中根据字典树进行单词查找。思路不难但是代码量还是很大的。

Code:

 

 1 struct TrieNode{
 2     TrieNode *next[26];
 3     bool isEnd;
 4     TrieNode():isEnd(false){
 5         for(int i = 0;i != 26;++i)
 6             next[i] = NULL;
 7     }
 8 };
 9 class TrieTree{
10 public:
11     TrieTree(){
12         root = new TrieNode();
13     }
14     
15     void insert(string word) {
16         TrieNode *current = root;
17         for(int i = 0;i != word.size();++i){
18             if(current->next[word[i]-a] == NULL)
19                 current->next[word[i]-a] = new TrieNode();
20             current = current->next[word[i]-a];
21         }
22         current->isEnd = true;
23     }
24     struct TrieNode *getRoot(){
25         return root;
26     }
27 private:
28     TrieNode *root;
29 };
30 class Solution {
31 public:
32     void dfs(vector<vector<char>>& board,vector<vector<bool>>& visited,TrieNode *current,int x,int y,string path,unordered_set<string>& result){
33         if(current->isEnd) {
34             result.insert(path);
35         }
36         visited[x][y] = true;
37         if(x-1 >= 0 && !visited[x-1][y] && current->next[board[x-1][y]-a] != NULL)
38             dfs(board,visited,current->next[board[x-1][y]-a],x-1,y,path+board[x-1][y],result);
39         if(x+1 < board.size() && !visited[x+1][y] && current->next[board[x+1][y]-a] != NULL)
40             dfs(board,visited,current->next[board[x+1][y]-a],x+1,y,path+board[x+1][y],result);
41         if(y-1 >= 0 && !visited[x][y-1] && current->next[board[x][y-1]-a] != NULL)
42             dfs(board,visited,current->next[board[x][y-1]-a],x,y-1,path+board[x][y-1],result);
43         if(y+1 < board[0].size() && !visited[x][y+1] && current->next[board[x][y+1]-a] != NULL)
44             dfs(board,visited,current->next[board[x][y+1]-a],x,y+1,path+board[x][y+1],result);
45         visited[x][y] = false;
46     }
47     vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
48         TrieTree tree;
49         for(int i = 0;i != words.size();++i)
50             tree.insert(words[i]);
51         unordered_set<string> result;
52         TrieNode *root = tree.getRoot();
53         int m = board.size();
54         int n = board[0].size();
55         vector<vector<bool>> visited(m,vector<bool>(n,false));
56         for(int i = 0;i != board.size();++i){
57             for(int j = 0;j != board[0].size();++j){
58                 if(root->next[board[i][j]-a] != NULL){
59                     string path = "";
60                     path += board[i][j];
61                     TrieNode *current = root->next[board[i][j]-a];
62                     dfs(board,visited,current,i,j,path,result);
63                 }
64             }
65         }
66         vector<string> res(result.begin(),result.end());
67         return res;
68     }
69 };

 


以上是关于Leetcode 212. Word Search II的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 212. Word Search II Add to List 查找单词---------- java

79. Word Search

212. Word Search II

212. Word Search II

212. Word Search II

212. Word Search II