-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.cpp
More file actions
163 lines (141 loc) · 4.58 KB
/
Copy pathsolution.cpp
File metadata and controls
163 lines (141 loc) · 4.58 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
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
/**
* Problem: 212. Word Search II
* Difficulty: Hard
* Topics: Array, String, Backtracking, Trie, Matrix
* LeetCode Link: https://leetcode.com/problems/word-search-ii/
*
* Time Complexity: O(M * N * 3^L + W * L) where M, N <= 12, L <= 10 (max word length), W = words.size()
* Space Complexity: O(W * L) for the Trie data structure
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cassert>
using namespace std;
struct TrieNode {
TrieNode* children[26];
string word;
int childCount;
TrieNode() : word(""), childCount(0) {
for (int i = 0; i < 26; ++i) {
children[i] = nullptr;
}
}
};
class Solution {
private:
void insert(TrieNode* root, const string& w) {
TrieNode* curr = root;
for (char ch : w) {
int idx = ch - 'a';
if (curr->children[idx] == nullptr) {
curr->children[idx] = new TrieNode();
curr->childCount++;
}
curr = curr->children[idx];
}
curr->word = w;
}
void dfs(vector<vector<char>>& board, int r, int c, TrieNode* parent, int parentChildIdx,
vector<string>& result) {
char ch = board[r][c];
int idx = ch - 'a';
TrieNode* currNode = parent->children[idx];
if (!currNode) return;
// If a word ends at this node, record it and clear to prevent duplicates
if (!currNode->word.empty()) {
result.push_back(currNode->word);
currNode->word.clear();
}
// Temporarily mark cell as visited
board[r][c] = '#';
static const int dr[4] = {-1, 1, 0, 0};
static const int dc[4] = {0, 0, -1, 1};
int m = static_cast<int>(board.size());
int n = static_cast<int>(board[0].size());
for (int d = 0; d < 4; ++d) {
int nr = r + dr[d];
int nc = c + dc[d];
if (nr >= 0 && nr < m && nc >= 0 && nc < n && board[nr][nc] != '#') {
int nextIdx = board[nr][nc] - 'a';
if (currNode->children[nextIdx] != nullptr) {
dfs(board, nr, nc, currNode, nextIdx, result);
}
}
}
// Backtrack: restore character
board[r][c] = ch;
// Trie pruning: if this node is now a leaf and has no associated word, unlink it
if (currNode->childCount == 0 && currNode->word.empty()) {
parent->children[parentChildIdx] = nullptr;
parent->childCount--;
delete currNode;
}
}
void freeTrie(TrieNode* node) {
if (!node) return;
for (int i = 0; i < 26; ++i) {
if (node->children[i]) {
freeTrie(node->children[i]);
}
}
delete node;
}
public:
vector<string> findWords(vector<vector<char>>& board, vector<string>& words) {
TrieNode* root = new TrieNode();
for (const string& w : words) {
insert(root, w);
}
vector<string> result;
int m = static_cast<int>(board.size());
int n = static_cast<int>(board[0].size());
for (int r = 0; r < m; ++r) {
for (int c = 0; c < n; ++c) {
int idx = board[r][c] - 'a';
if (root->children[idx] != nullptr) {
dfs(board, r, c, root, idx, result);
}
}
}
freeTrie(root);
return result;
}
};
// ==========================================
// Local Test Runner (Guarded for LeetCode Submission)
// ==========================================
#ifdef LOCAL_TEST
int main() {
Solution solver;
// Test Case 1: Example 1
{
vector<vector<char>> board = {
{'o', 'a', 'a', 'n'},
{'e', 't', 'a', 'e'},
{'i', 'h', 'k', 'r'},
{'i', 'f', 'l', 'v'}
};
vector<string> words = {"oath", "pea", "eat", "rain"};
auto result = solver.findWords(board, words);
sort(result.begin(), result.end());
vector<string> expected = {"eat", "oath"};
assert(result == expected);
cout << "Test 1 Passed: [\"eat\", \"oath\"] found!" << endl;
}
// Test Case 2: Example 2
{
vector<vector<char>> board = {
{'a', 'b'},
{'c', 'd'}
};
vector<string> words = {"abcb"};
auto result = solver.findWords(board, words);
assert(result.empty());
cout << "Test 2 Passed: [\"abcb\"] not found!" << endl;
}
cout << "All test cases passed successfully!" << endl;
return 0;
}
#endif