Skip to content

Commit 7b8108f

Browse files
Create WordSearchll .cpp
1 parent 381409e commit 7b8108f

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
//Given a 2D board and a list of words from the dictionary, find all words in the board.
2+
//Each word must be constructed from letters of sequentially adjacent cell, where "adjacent"
3+
//cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
4+
5+
6+
7+
8+
#include <bits/stdc++.h>
9+
using namespace std;
10+
11+
const int ALPHABET_SIZE = 26;
12+
13+
// trie node
14+
15+
class Solution {
16+
public:
17+
vector<string>ret;
18+
struct TrieNode
19+
{
20+
struct TrieNode *children[ALPHABET_SIZE];
21+
bool isEndOfWord;
22+
};
23+
24+
struct TrieNode *getNode(void)
25+
{
26+
struct TrieNode *pNode = new TrieNode;
27+
28+
pNode->isEndOfWord = false;
29+
30+
for (int i = 0; i < ALPHABET_SIZE; i++)
31+
pNode->children[i] = NULL;
32+
33+
return pNode;
34+
}
35+
void insert(struct TrieNode *root, string key)
36+
{
37+
struct TrieNode *pCrawl = root;
38+
39+
for (int i = 0; i < key.length(); i++)
40+
{
41+
int index = key[i] - 'a';
42+
if (!pCrawl->children[index])
43+
pCrawl->children[index] = getNode();
44+
45+
pCrawl = pCrawl->children[index];
46+
}
47+
pCrawl->isEndOfWord = true;
48+
}
49+
int search(struct TrieNode *root, string key)
50+
{
51+
struct TrieNode *pCrawl = root;
52+
53+
for (int i = 0; i < key.length(); i++)
54+
{
55+
int index = key[i] - 'a';
56+
if (!pCrawl->children[index])
57+
return 0;
58+
59+
pCrawl = pCrawl->children[index];
60+
}
61+
if(pCrawl->isEndOfWord==false && pCrawl!=NULL)
62+
return 1;
63+
if(pCrawl != NULL && pCrawl->isEndOfWord)
64+
return 2;
65+
return -1;
66+
}
67+
void fn(struct TrieNode*root,vector<vector<char>>& board,string str,int i,int j)
68+
{
69+
char store =board[i][j];
70+
board[i][j]='0';
71+
int temp=0;
72+
if(i-1 >=0 && board[i-1][j]!='0')
73+
{
74+
75+
str.push_back(board[i-1][j]);
76+
backtrack(root,board,str,i-1,j);
77+
str.pop_back();
78+
board[i][j]=store;
79+
}
80+
if(j-1 >=0 && board[i][j-1]!='0')
81+
{
82+
str.push_back(board[i][j-1]);
83+
backtrack(root,board,str,i,j-1);
84+
str.pop_back();
85+
board[i][j]=store;
86+
}
87+
if(i+1 < board.size() && board[i+1][j]!='0')
88+
{
89+
str.push_back(board[i+1][j]);
90+
backtrack(root,board,str,i+1,j);
91+
str.pop_back();
92+
board[i][j]=store;
93+
94+
}
95+
if(j+1 <board[0].size() && board[i][j+1]!='0')
96+
{
97+
str.push_back(board[i][j+1]);
98+
backtrack(root,board,str,i,j+1);
99+
str.pop_back();
100+
board[i][j]=store;
101+
102+
}
103+
board[i][j]=store;
104+
}
105+
int backtrack(struct TrieNode*root,vector<vector<char>>& board,string str,int i,int j)
106+
{
107+
if(search(root,str)==0)
108+
{
109+
return 0;
110+
}
111+
else
112+
{
113+
if(search(root,str)==2)
114+
{
115+
ret.push_back(str);
116+
}
117+
char store =board[i][j];
118+
board[i][j]='0';
119+
int temp=0;
120+
if(i-1 >=0 && board[i-1][j]!='0')
121+
{
122+
board[i][j]='0';
123+
str.push_back(board[i-1][j]);
124+
temp=backtrack(root,board,str,i-1,j);
125+
str.pop_back();
126+
board[i][j]=store;
127+
}
128+
if(j-1 >=0 && board[i][j-1]!='0')
129+
{
130+
board[i][j]='0';
131+
str.push_back(board[i][j-1]);
132+
temp=backtrack(root,board,str,i,j-1);
133+
str.pop_back();
134+
board[i][j]=store;
135+
}
136+
if(i+1 < board.size() && board[i+1][j]!='0')
137+
{
138+
board[i][j]='0';
139+
str.push_back(board[i+1][j]);
140+
temp=backtrack(root,board,str,i+1,j);
141+
str.pop_back();
142+
board[i][j]=store;
143+
}
144+
if(j+1 <board[0].size() && board[i][j+1]!='0')
145+
{
146+
board[i][j]='0';
147+
str.push_back(board[i][j+1]);
148+
temp=backtrack(root,board,str,i,j+1);
149+
str.pop_back();
150+
board[i][j]=store;
151+
152+
}
153+
board[i][j]=store;
154+
return temp;
155+
156+
}
157+
}
158+
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
159+
int n = words.size();
160+
if(words.size()==0)
161+
{
162+
vector<string>r;
163+
return r;
164+
}
165+
struct TrieNode *root = getNode();
166+
vector<vector<char>>copy=board;
167+
// Construct trie
168+
for (int i = 0; i < n; i++)
169+
insert(root, words[i]);
170+
for(int i=0;i<board.size();i++)
171+
for(int j=0;j<board[0].size();j++)
172+
{
173+
copy=board;
174+
string str(1,board[i][j]);
175+
backtrack(root,copy,str,i,j);
176+
177+
}
178+
vector<string>::iterator ip;
179+
if(ret.size()==0)
180+
return ret;
181+
sort(ret.begin(),ret.end());
182+
vector<string>final;
183+
for (int i=1;i<ret.size();i++) {
184+
if(ret[i-1]!=ret[i])
185+
final.push_back(ret[i-1]);
186+
}
187+
final.push_back(ret[ret.size()-1]);
188+
return final;
189+
}
190+
};

0 commit comments

Comments
 (0)