@@ -249,3 +249,135 @@ class Solution {
249
249
}
250
250
}
251
251
```
252
+
253
+ ### C++ version
254
+ * Method 1: dfs
255
+ ``` objectivec
256
+ class Solution {
257
+ private:
258
+ vector<string > res_ ;
259
+ static int dir[ 4] [ 2 ] ;
260
+ vector<vector<char >> board_ ;
261
+ int height_ , width_ ;
262
+ bool dfs(int index, const string& word, int row, int col){
263
+ if(index == word.length()) return true;
264
+ int tx = 0, ty = 0;
265
+ for(int d = 0; d < 4; ++d){
266
+ tx = row + Solution::dir[ d] [ 0 ] ;
267
+ ty = col + Solution::dir[ d] [ 1 ] ;
268
+ if(tx >= 0 && tx < height_ && ty >= 0 && ty < width_ && board_ [ tx] [ ty ] != '#' && board_ [ tx] [ ty ] == word[ index] ){
269
+ char c = board_ [ tx] [ ty ] ;
270
+ board_ [ tx] [ ty ] = '#';
271
+ if(dfs(index + 1, word, tx, ty)){
272
+ board_ [ tx] [ ty ] = c;
273
+ return true;
274
+ }
275
+ board_ [ tx] [ ty ] = c;
276
+ }
277
+ }
278
+ return false;
279
+ }
280
+ public:
281
+ vector<string > findWords(vector<vector<char >>& board, vector<string >& words) {
282
+ height_ = board.size();
283
+ width_ = board[ 0] .size();
284
+ board_ = move(board);
285
+ unordered_set<string > set(words.begin(), words.end());
286
+ for(const string& word: set){
287
+ bool found = false;
288
+ for(int i = 0; i < height_ && !found; ++i){
289
+ for(int j = 0; j < width_ && !found; ++j){
290
+ if(word[ 0] == board_ [ i] [ j ] ){
291
+ char c = board_ [ i] [ j ] ;
292
+ board_ [ i] [ j ] = '#';
293
+ if(dfs(1, word, i, j)){
294
+ res_ .emplace_back(word);
295
+ found = true;
296
+ }
297
+ board_ [ i] [ j ] = c;
298
+ }
299
+ }
300
+ }
301
+ }
302
+ return res_ ;
303
+ }
304
+ };
305
+ int Solution::dir[4 ][2 ] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
306
+ static auto speedup=[ ] ( ) {
307
+ ios::sync_with_stdio (false);
308
+ cin.tie(nullptr);
309
+ cout.tie(nullptr);
310
+ return nullptr;
311
+ }();
312
+ ```
313
+
314
+ * Method 2: Tire Tree
315
+ ```objectivec
316
+ struct Node{
317
+ vector<Node*> childs;
318
+ const string* word;
319
+
320
+ Node(): childs(26), word(nullptr){}
321
+ ~Node(){
322
+ for(auto node: childs){
323
+ delete node;
324
+ }
325
+ }
326
+ };
327
+ class Solution {
328
+ private:
329
+ unique_ptr<Node> root_;
330
+
331
+ void insert(const string& word){
332
+ Node* temp = root_.get();
333
+ for(const char& c: word){
334
+ if(!temp->childs[c - 'a']){
335
+ temp->childs[c - 'a'] = new Node();
336
+ }
337
+ temp = temp->childs[c - 'a'];
338
+ }
339
+ temp->word = &word;
340
+ }
341
+ static int dir[4][2];
342
+ public:
343
+ vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
344
+ root_.reset(new Node());
345
+ for(const string& word: words){
346
+ insert(word);
347
+ }
348
+ int height = board.size(), width = board[0].size();
349
+ vector<string> res;
350
+ function<void(int, int, Node*)> walk = [&](int i, int j, Node* node){
351
+ if(i < 0 || i >= height || j < 0 || j >= width || board[i][j] == '#') return;
352
+ const char c = board[i][j];
353
+ Node* next = node->childs[c - 'a'];
354
+ if(!next) return;
355
+ if(next->word){
356
+ res.emplace_back(*next->word);
357
+ next->word = nullptr;
358
+ }
359
+ int tx = 0, ty = 0;
360
+ board[i][j] = '#';
361
+ for(int d = 0; d < 4; ++d){
362
+ tx = i + dir[d][0];
363
+ ty = j + dir[d][1];
364
+ walk(tx, ty, next);
365
+ }
366
+ board[i][j] = c;
367
+ };
368
+ for(int i = 0; i < height; ++i){
369
+ for(int j = 0; j < width; ++j){
370
+ walk(i, j, root_.get());
371
+ }
372
+ }
373
+ return res;
374
+ }
375
+ };
376
+ int Solution::dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
377
+ static auto speedup=[](){
378
+ ios::sync_with_stdio(false);
379
+ cin.tie(nullptr);
380
+ cout.tie(nullptr);
381
+ return nullptr;
382
+ }();
383
+ ```
0 commit comments