-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwordboogie.cpp
230 lines (188 loc) · 5.54 KB
/
wordboogie.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
class Solution {
public:
bool search(vector<vector<char> >& board, const string& word, int index, int x,
int y) {
// If position of the cell is out of boundary of board or
// letter in current cell does not match letter of word
if (x < 0 || x == board[0].size() || y < 0 || y == board.size() ||
word[index] != board[y][x])
return false;
// Base case : each character of the word has been matched
if (index == word.length() - 1) return true;
// mark the current cell as visited
char cur = board[y][x];
board[y][x] = '#';
bool found = search(board, word, index + 1, x + 1, y) // bottom
|| search(board, word, index + 1, x - 1, y) // top
|| search(board, word, index + 1, x, y + 1) // right
|| search(board, word, index + 1, x, y - 1) // left
|| search(board, word, index + 1, x + 1, y + 1) // bottom right
|| search(board, word, index + 1, x - 1, y + 1) // top right
|| search(board, word, index + 1, x + 1, y - 1) // bottom left
|| search(board, word, index + 1, x - 1, y - 1); // top left
// revert the current cell back to its original state
board[y][x] = cur;
return found;
}
bool exist(vector<vector<char> >& board, string word) {
if (board.size() == 0) return false;
// Consider every character cell and look for all words
// starting with this character
for (int i = 0; i < board[0].size(); i++)
for (int j = 0; j < board.size(); j++)
if (search(board, word, 0, i, j)) return true;
return false;
}
vector<string> wordBoggle(vector<vector<char> >& board,
vector<string>& dictionary) {
vector<string> result;
// Iterate through every word in the dictionary
for (int i = 0; i < dictionary.size(); ++i) {
string word = dictionary[i];
if (exist(board, word)) result.push_back(word);
}
return result;
}
};
2nd __code_model_32__
trie search void
insert(struct TrieNode *root, string key)
{
int level;
int length = key.size();
int index;
struct TrieNode *pCrawl = root;
for (level = 0; level < length; level++)
{
index = CHAR_TO_INDEX(key[level]);
if (!pCrawl->children[index])
pCrawl->children[index] = getNode();
pCrawl = pCrawl->children[index];
}
// mark last node as leaf
pCrawl->isLeaf = true;
}
// Returns true if key presents in trie, else false
bool search(struct TrieNode *root, string key)
{
int level;
int length = key.size();
int index;
struct TrieNode *pCrawl = root;
for (level = 0; level < length; level++)
{
index = CHAR_TO_INDEX(key[level]);
if (!pCrawl->children[index])
return false;
pCrawl = pCrawl->children[index];
}
return (pCrawl != NULL && pCrawl->isLeaf);
}
trie delete
class Solution
{
public:
int leafNode(trie_node_t *pNode)
{
return (pNode->value != 0);
}
int isItFreeNode(trie_node_t *pNode)
{
int i;
for (i = 0; i < ALPHABET_SIZE; i++)
{
if (pNode->children[i])
return 0;
}
return 1;
}
void remove(trie_node_t *root, char key[], int level, int len)
{
// If tree is empty
if (!root)
return;
// If last character of key is being processed
if (level == len)
{
// This node is no more end of word after
// removal of given key
if (root->value)
root->value = 0;
// If given is not prefix of any other word
if (isItFreeNode(root))
{
root = NULL;
}
return;
}
// If not last character, recur for the child
// obtained using ASCII value
int index = INDEX(key[level + 1]);
remove(root->children[index], key, level + 1, len);
// If root does not have any child (its only child got
// deleted), and it is not end of another word.
if (isItFreeNode(root) && root->value == false)
{
root = NULL;
}
}
void deleteKey(trie_node_t *root, char key[])
{
int len = strlen(key);
if (len > 0)
{
remove(root->children[INDEX(key[0])], key, 0, len - 1);
}
}
};
hamilton class Solution
{
public:
vector<vector<int>> V;
bool dfs(int v, int label[15], int count, int N)
{
label[v] = 1;
if (count == N)
return true;
for (int j = 0; j < V[v].size(); j++)
{
if (label[V[v][j]] == 0)
{
if (dfs(V[v][j], label, count + 1, N))
return true;
label[V[v][j]] = 0;
}
}
return false;
}
bool check(int N, int M, vector<vector<int>> Edges)
{
V = vector<vector<int>>(N + 1);
for (auto i : Edges)
{
V[i[0]].push_back(i[1]);
V[i[1]].push_back(i[0]);
}
int label[15];
for (int i = 0; i <= N; i++)
label[i] = 0;
for (int i = 0; i < N; i++)
{
if (dfs(i, label, 1, N))
return true;
label[i] = 0;
}
return false;
}
};
transaction_safe_dynamic
class Solution{
public :
int numsGame(int N){
// Tom wins if N is even
if (N % 2 == 0) return 1;
// else Jerry wins
else return 0;
}
}
;