-
Notifications
You must be signed in to change notification settings - Fork 46
/
Trie1.cpp
64 lines (54 loc) · 1.99 KB
/
Trie1.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
class Solution {
public:
struct trie{
trie* next[26];
int end;
};
void insert( string word , trie* root ){
for ( char c : word){
if ( !root->next[c-'a'] ) root->next[c-'a'] = new trie();
root = root->next[c-'a'];
}
root->end =1;
}
void rec( vector<vector<int>> &vis , vector<vector<char>> &board , int x , int y , trie* root ,string curr, set<string> &ans ){
int n = board.size();
int m = board[0].size();
// base condition for return
if (x < 0 || y < 0 || x >= n || y >= m ) return ;
// already done in current recursion
if ( vis[x][y] ) return;
// if no such word exists it is useless to recurse
if (!root || !root -> next[board[x][y] - 'a']) return ;
root = root -> next[board[x][y]-'a'];
curr.push_back(board[x][y]);
if ( root->end == 1)
ans.insert(curr);
//however we still don't end recursion for words like "end" and "ending"
vis[x][y] =1;
rec( vis , board , x + 1, y , root ,curr , ans);
rec( vis , board , x - 1, y , root ,curr , ans);
rec( vis , board , x , y -1, root ,curr ,ans);
rec( vis , board , x , y +1 , root ,curr , ans);
vis[x][y] =0;
curr.pop_back();
}
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
trie * root = new trie();
for ( string word : words) {
insert( word , root);
}
set<string> ans;
string curr;
int n = board.size();
int m = board[0].size();
vector<vector<int> > vis( n , vector<int> ( m , 0 ));
for ( int i =0 ; i < n ; i++ )
for ( int j = 0 ; j < m ; j++)
rec( vis ,board, i , j , root ,curr, ans);
vector<string> ret;
for ( string a : ans)
ret.push_back(a);
return ret;
}
};