You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Given a 2D board and a list of words from the dictionary, find all words in the board.
7
+
8
+
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.
9
+
10
+
11
+
12
+
Example:
13
+
14
+
Input:
15
+
board = [
16
+
['o','a','a','n'],
17
+
['e','t','a','e'],
18
+
['i','h','k','r'],
19
+
['i','f','l','v']
20
+
]
21
+
words = ["oath","pea","eat","rain"]
22
+
23
+
Output: ["eat","oath"]
24
+
25
+
26
+
Note:
27
+
28
+
All inputs are consist of lowercase letters a-z.
29
+
The values of words are distinct.
30
+
31
+
*/
32
+
33
+
classSolution {
34
+
classTrie{
35
+
public:
36
+
Trie *children[26]; // pointers to its substrings starting with 'a' to 'z'
37
+
bool leaf; // if the node is a leaf, or if there is a word stopping at here
38
+
int idx; // if it is a leaf, the string index of the array words
39
+
Trie()
40
+
{
41
+
this->leaf = false;
42
+
this->idx = 0;
43
+
fill_n(this->children, 26, nullptr);
44
+
}
45
+
};
46
+
47
+
public:
48
+
voidinsertWords(Trie *root, vector<string>& words, int idx)
0 commit comments