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

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 clear :-)

 1 class TrieNode {
 2 public:
 3     bool isKey;
 4     TrieNode* children[26];
 5     TrieNode() : isKey(false) {
 6         memset(children, NULL, sizeof(TrieNode*) * 26);
 7     }
 8 };
 9 
10 class Trie {
11 public:
12     Trie() {
13         root = new TrieNode();
14     }
15     
16     void add(string& word) {
17         TrieNode* run = root;
18         for (char c : word) {
19             if (!(run -> children[c - 'a']))
20                 run -> children[c - 'a'] = new TrieNode();
21             run = run -> children[c - 'a'];
22         }
23         run -> isKey = true;
24     }
25     
26     bool search(string& word) {
27         TrieNode* p = query(word);
28         return p && p -> isKey;
29     }
30     
31     bool startsWith(string& prefix) {
32         return query(prefix) != NULL;
33     }
34 private:
35     TrieNode* root;
36     TrieNode* query(string& s) {
37         TrieNode* run = root;
38         for (char c : s) {
39             if (!(run -> children[c - 'a'])) return NULL;
40             run = run -> children[c - 'a'];
41         }
42         return run;
43     }
44 };
45 
46 class Solution {
47 public:
48     vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
49         Trie trie = Trie();
50         for (string word : words) trie.add(word);
51         int m = board.size(), n = board[0].size();
52         for (int r = 0; r < m; r++)
53             for (int c = 0; c < n; c++)
54                 find(trie, board, "", r, c);
55         return vector<string> (st.begin(), st.end());
56     }
57 private:
58     unordered_set<string> st;
59     void find(Trie trie, vector<vector<char>>& board, string word, int r, int c) {
60         int m = board.size(), n = board[0].size();
61         if (board[r][c] == '%') return;
62         word += board[r][c];
63         if (!trie.startsWith(word)) return;
64         if (trie.search(word)) st.insert(word);
65         board[r][c] = '%';
66         if (r) find(trie, board, word, r - 1, c);
67         if (r < m - 1) find(trie, board, word, r + 1, c);
68         if (c) find(trie, board, word, r, c - 1);
69         if (c < n - 1) find(trie, board, word, r, c + 1);
70         board[r][c] = word.back();
71     }
72 };

 

目录
相关文章
|
6月前
Leetcode 74. Search a 2D Matrix
这道题很简单,为此专门写篇博客其实算博客凑数了。给你一个每一行每一列都是增序,且每一行第一个数都大于上一行末尾数的矩阵,让你判断某个数在这个矩阵中是否存在。 假设矩阵是m*n,扫一遍的时间复杂度就是O(m*n),题目中给出的这么特殊的矩阵,时间复杂度可以降到O(m+n),具体代码如下,写的比较挫。
62 1
|
算法 Python
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 108. 将有序数组转换为二叉搜索树 Convert Sorted Array to Binary Search Tree
LeetCode 318. Maximum Product of Word Lengths
给定一个字符串数组 words,找到 length(word[i]) * length(word[j]) 的最大值,并且这两个单词不含有公共字母。你可以认为每个单词只包含小写字母。如果不存在这样的两个单词,返回 0。
52 0
LeetCode 318. Maximum Product of Word Lengths
LeetCode 290. Word Pattern
给定一种 pattern(模式) 和一个字符串 str ,判断 str 是否遵循相同的模式。 这里的遵循指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接的对应模式。
31 0
LeetCode 290. Word Pattern
LeetCode 212. Word Search II
给定一个二维网格 board 和一个字典中的单词列表 words,找出所有同时在二维网格和字典中出现的单词。 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母在一个单词中不允许被重复使用。
55 0
LeetCode 212. Word Search II
|
索引
LeetCode 81. Search in Rotated Sorted Array II
假设按升序排序的数组在事先未知的某个枢轴处旋转。 (例如, [0,0,1,2,2,5,6] 可能变成 [2,5,6,0,0,1,2]). 给定一个要搜索的目标值T, 如果该目标值能在数组中找到则返回true,否则返回false。
64 0
LeetCode 81. Search in Rotated Sorted Array II
|
索引
LeetCode 79. Word Search
给定一个2D板和一个单词,找出该单词是否存在于网格中。 该单词可以由顺序相邻的单元的字母构成,其中“相邻”单元是水平或垂直相邻的单元。 相同的字母单元格不得多次使用。
49 0
LeetCode 79. Word Search
|
算法
LeetCode 74. Search a 2D Matrix
编写一个有效的算法,搜索m×n矩阵中的值。 此矩阵具有以下属性: 每行中的整数从左到右排序. 每行的第一个整数大于前一行的最后一个整数.
50 0
LeetCode 74. Search a 2D Matrix
|
机器学习/深度学习
Leetcode-Medium 96.Unique Binary Search Trees
Leetcode-Medium 96.Unique Binary Search Trees
77 0
|
算法 Python
Leetcode-Medium 98. Validate Binary Search Tree
Leetcode-Medium 98. Validate Binary Search Tree
108 0

热门文章

最新文章