-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDay-30-Word Search II.cpp
135 lines (97 loc) · 3.37 KB
/
Day-30-Word Search II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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:
board = [
['o','a','a','n'],
['e','t','a','e'],
['i','h','k','r'],
['i','f','l','v']
]
words = ["oath","pea","eat","rain"]
Output: ["eat","oath"]
Note:
All inputs are consist of lowercase letters a-z.
The values of words are distinct.
Hide Hint #1
You would need to optimize your backtracking to pass the larger test. Could you stop backtracking earlier?
Hide Hint #2
If the current candidate does not exist in all words' prefix,
you could stop backtracking immediately.
What kind of data structure could answer such query efficiently?
Does a hash table work? Why or why not? How about a Trie?
If you would like to learn how to implement a basic trie,
please work on this problem: Implement Trie (Prefix Tree) first.
class Solution {
public:
struct TrieNode {
int ew; // end of word
struct TrieNode* child[26];
string word;
};
struct TrieNode* createNode(){
struct TrieNode* newNode=new TrieNode;
newNode->ew=0;
newNode->word="";
for(int i=0;i<26;i++)
newNode->child[i]=NULL;
return newNode;
}
void insert(struct TrieNode* root, string word){
struct TrieNode* cur=root;
int wordLen=word.length();
for(int i=0;i<wordLen;i++){
int index=word[i]-'a';
// create new node if not already exist
if(cur->child[index]==NULL)
cur->child[index]=createNode();
// point to new node
cur=cur->child[index];
}
cur->ew+=1; // end of word
cur->word=word;
}
void solve(vector<vector<char>>& board, int i, int j, int row, int col, vector<string> &res, struct TrieNode* cur){
// base case
int index=board[i][j]-'a';
if(board[i][j]=='$' || cur->child[index]==NULL) return;
cur=cur->child[index];
if(cur->ew>0){
res.push_back(cur->word);
cur->ew-=1;
}
char ch=board[i][j];
board[i][j]='$'; // mark visited
if(i>0) // top
solve(board, i-1, j, row, col, res, cur);
if(i<row-1) // down
solve(board, i+1, j, row, col, res, cur);
if(j>0) //left
solve(board, i, j-1, row, col, res, cur);
if(j<col-1) // right
solve(board, i, j+1, row, col, res, cur);
board[i][j]=ch;
}
static bool comp(string a, string b){
int tmp=a.compare(b);
return tmp<=0 ? true: false;
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
int row=board.size();
int col=board[0].size();
vector<string> res;
struct TrieNode* root=createNode(); // root
// insert words in TRIE
int wordLen=words.size();
for(int i=0;i<wordLen;i++)
insert(root, words[i]);
// cout<<search(root, "eat");
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
solve(board, i, j, row, col, res, root);
}
}
sort(res.begin(), res.end(), comp);
return res;
}
};