-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLC212.cpp
More file actions
102 lines (73 loc) · 2.89 KB
/
Copy pathLC212.cpp
File metadata and controls
102 lines (73 loc) · 2.89 KB
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
/*
212. Word Search II
Given an m x n board of characters and a list of strings words, return all words on the board.
Each word must be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once in a word.
Example 1:
*/
#include <vector>
#include <string>
#include <unordered_map>
#include <memory>
class Solution {
struct Node {
// Use unique_ptr to automatically prevent memory leaks
std::unordered_map<char, std::unique_ptr<Node>> children;
std::string word = "";
};
std::unique_ptr<Node> trie;
// Using a std::pair array with structured bindings makes direction code readable
const std::vector<std::pair<int, int>> directions = {
{-1, 0}, {1, 0}, {0, -1}, {0, 1}
};
void dfs(std::vector<string>& result, std::vector<std::vector<char>>& board, int row, int col, Node* trieNode) {
char ch = board[row][col];
Node* next = trieNode->children[ch].get();
if (!next->word.empty()) {
result.push_back(std::move(next->word));
next->word.clear();
}
if (next->children.empty()) {
trieNode->children.erase(ch);
return;
}
board[row][col] = '#'; //ensuring we have visited
for (const auto& [dr, dc] : directions) {
int r = row + dr;
int c = col + dc;
if (r < 0 || r >= board.size() || c < 0 || c >= board[0].size()) {
continue;
}
if (next->children.contains(board[r][c])) {
dfs(result, board, r, c, next);
}
}
board[row][col] = ch; // put back from '#' to original char.
}
public:
Solution() : trie(std::make_unique<Node>()) {}
std::vector<std::string> findWords(vector<vector<char>>& board, vector<string>& words) {
// constructing a trie using list of input words
for (const auto& word : words) {
Node* curr = trie.get();
for (const char c : word) {
if (!curr->children.contains(c)) {
curr->children[c] = std::make_unique<Node>();
}
curr= curr->children[c].get();
}
curr->word = word;
}
//tracking trie untill we meet a completed word
std::vector<std::string> result;
int m = board.size();
int n = board[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (trie->children.contains(board[i][j])) {
dfs(result, board, i, j , trie.get());
}
}
}
return result;
}
};