|
45 | 45 |
|
46 | 46 | <!-- 这里可写通用的实现逻辑 -->
|
47 | 47 |
|
| 48 | +DFS。 |
| 49 | + |
48 | 50 | <!-- tabs:start -->
|
49 | 51 |
|
50 | 52 | ### **Python3**
|
51 | 53 |
|
52 | 54 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
53 | 55 |
|
54 | 56 | ```python
|
| 57 | +class Solution: |
| 58 | + def findWords(self, board: List[List[str]], words: List[str]) -> List[str]: |
| 59 | + def check(word): |
| 60 | + cnt = Counter(word) |
| 61 | + return all(counter[c] >= i for c, i in cnt.items()) |
| 62 | + |
| 63 | + def dfs(i, j, l, word): |
| 64 | + if l == len(word): |
| 65 | + return True |
| 66 | + if i < 0 or i >= m or j < 0 or j >= n or board[i][j] != word[l]: |
| 67 | + return False |
| 68 | + c = board[i][j] |
| 69 | + board[i][j] = '0' |
| 70 | + ans = False |
| 71 | + for a, b in [[0, -1], [0, 1], [1, 0], [-1, 0]]: |
| 72 | + x, y = i + a, j + b |
| 73 | + ans = ans or dfs(x, y, l + 1, word) |
| 74 | + board[i][j] = c |
| 75 | + return ans |
| 76 | + |
| 77 | + def find(word): |
| 78 | + if not check(word): |
| 79 | + return False |
| 80 | + for i in range(m): |
| 81 | + for j in range(n): |
| 82 | + if dfs(i, j, 0, word): |
| 83 | + return True |
| 84 | + return False |
55 | 85 |
|
| 86 | + m, n = len(board), len(board[0]) |
| 87 | + words = set(words) |
| 88 | + counter = Counter(c for b in board for c in b) |
| 89 | + return [word for word in words if find(word)] |
56 | 90 | ```
|
57 | 91 |
|
58 | 92 | ### **Java**
|
59 | 93 |
|
60 | 94 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
61 | 95 |
|
62 | 96 | ```java
|
| 97 | +class Solution { |
| 98 | + private int[] counter; |
| 99 | + private char[][] board; |
| 100 | + |
| 101 | + public List<String> findWords(char[][] board, String[] words) { |
| 102 | + counter = new int[26]; |
| 103 | + this.board = board; |
| 104 | + for (char[] b : board) { |
| 105 | + for (char c : b) { |
| 106 | + ++counter[c - 'a']; |
| 107 | + } |
| 108 | + } |
| 109 | + Set<String> s = new HashSet<>(Arrays.asList(words)); |
| 110 | + List<String> ans = new ArrayList<>(); |
| 111 | + for (String word : s) { |
| 112 | + if (find(word)) { |
| 113 | + ans.add(word); |
| 114 | + } |
| 115 | + } |
| 116 | + return ans; |
| 117 | + } |
| 118 | + |
| 119 | + private boolean find(String word) { |
| 120 | + if (!check(word)) { |
| 121 | + return false; |
| 122 | + } |
| 123 | + for (int i = 0; i < board.length; ++i) { |
| 124 | + for (int j = 0; j < board[0].length; ++j) { |
| 125 | + if (dfs(i, j, 0, word)) { |
| 126 | + return true; |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + private boolean dfs(int i, int j, int l, String word) { |
| 134 | + if (l == word.length()) { |
| 135 | + return true; |
| 136 | + } |
| 137 | + if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || board[i][j] != word.charAt(l)) { |
| 138 | + return false; |
| 139 | + } |
| 140 | + char c = board[i][j]; |
| 141 | + board[i][j] = '0'; |
| 142 | + boolean ans = false; |
| 143 | + int[] dirs = {-1, 0, 1, 0, -1}; |
| 144 | + for (int k = 0; k < 4; ++k) { |
| 145 | + int x = i + dirs[k]; |
| 146 | + int y = j + dirs[k + 1]; |
| 147 | + ans = ans || dfs(x, y, l + 1, word); |
| 148 | + } |
| 149 | + board[i][j] = c; |
| 150 | + return ans; |
| 151 | + } |
| 152 | + |
| 153 | + private boolean check(String word) { |
| 154 | + int[] cnt = new int[26]; |
| 155 | + for (char c : word.toCharArray()) { |
| 156 | + ++cnt[c - 'a']; |
| 157 | + } |
| 158 | + for (int i = 0; i < 26; ++i) { |
| 159 | + if (counter[i] < cnt[i]) { |
| 160 | + return false; |
| 161 | + } |
| 162 | + } |
| 163 | + return true; |
| 164 | + } |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +### **C++** |
| 169 | + |
| 170 | +```cpp |
| 171 | +class Solution { |
| 172 | +public: |
| 173 | + vector<int> counter; |
| 174 | + |
| 175 | + vector<string> findWords(vector<vector<char>>& board, vector<string>& words) { |
| 176 | + counter.resize(26); |
| 177 | + for (auto& b : board) |
| 178 | + for (auto& c : b) |
| 179 | + ++counter[c - 'a']; |
| 180 | + unordered_set<string> s(words.begin(), words.end()); |
| 181 | + vector<string> ans; |
| 182 | + for (string word : s) |
| 183 | + if (find(word, board)) |
| 184 | + ans.push_back(word); |
| 185 | + return ans; |
| 186 | + } |
| 187 | + |
| 188 | + bool find(string& word, vector<vector<char>>& board) { |
| 189 | + if (!check(word)) return false; |
| 190 | + for (int i = 0; i < board.size(); ++i) |
| 191 | + for (int j = 0; j < board[0].size(); ++j) |
| 192 | + if (dfs(i, j, 0, word, board)) |
| 193 | + return true; |
| 194 | + return false; |
| 195 | + } |
| 196 | + |
| 197 | + bool dfs(int i, int j, int l, string& word, vector<vector<char>>& board) { |
| 198 | + if (l == word.size()) return true; |
| 199 | + if (i < 0 || i >= board.size() || j < 0 || j >= board[0].size() || board[i][j] != word[l]) return false; |
| 200 | + char c = board[i][j]; |
| 201 | + board[i][j] = '0'; |
| 202 | + bool ans = false; |
| 203 | + vector<int> dirs = {-1, 0, 1, 0, -1}; |
| 204 | + for (int k = 0; k < 4; ++k) |
| 205 | + { |
| 206 | + int x = i + dirs[k], y = j + dirs[k + 1]; |
| 207 | + ans = ans || dfs(x, y, l + 1, word, board); |
| 208 | + } |
| 209 | + board[i][j] = c; |
| 210 | + return ans; |
| 211 | + } |
| 212 | + |
| 213 | + bool check(string word) { |
| 214 | + vector<int> cnt(26); |
| 215 | + for (char c : word) |
| 216 | + ++cnt[c - 'a']; |
| 217 | + for (int i = 0; i < 26; ++i) |
| 218 | + if (counter[i] < cnt[i]) |
| 219 | + return false; |
| 220 | + return true; |
| 221 | + } |
| 222 | +}; |
| 223 | +``` |
| 224 | +
|
| 225 | +### **Go** |
| 226 | +
|
| 227 | +```go |
| 228 | +func findWords(board [][]byte, words []string) []string { |
| 229 | + counter := make([]int, 26) |
| 230 | + for _, b := range board { |
| 231 | + for _, c := range b { |
| 232 | + counter[c-'a']++ |
| 233 | + } |
| 234 | + } |
| 235 | + s := make(map[string]bool) |
| 236 | + for _, word := range words { |
| 237 | + s[word] = true |
| 238 | + } |
| 239 | +
|
| 240 | + check := func(word string) bool { |
| 241 | + cnt := make([]int, 26) |
| 242 | + for i := range word { |
| 243 | + cnt[word[i]-'a']++ |
| 244 | + } |
| 245 | + for i := 0; i < 26; i++ { |
| 246 | + if counter[i] < cnt[i] { |
| 247 | + return false |
| 248 | + } |
| 249 | + } |
| 250 | + return true |
| 251 | + } |
| 252 | +
|
| 253 | + var dfs func(i, j, l int, word string) bool |
| 254 | + dfs = func(i, j, l int, word string) bool { |
| 255 | + if l == len(word) { |
| 256 | + return true |
| 257 | + } |
| 258 | + if i < 0 || i >= len(board) || j < 0 || j >= len(board[0]) || board[i][j] != word[l] { |
| 259 | + return false |
| 260 | + } |
| 261 | + c := board[i][j] |
| 262 | + board[i][j] = '0' |
| 263 | + ans := false |
| 264 | + dirs := []int{-1, 0, 1, 0, -1} |
| 265 | + for k := 0; k < 4; k++ { |
| 266 | + x, y := i+dirs[k], j+dirs[k+1] |
| 267 | + ans = ans || dfs(x, y, l+1, word) |
| 268 | + } |
| 269 | + board[i][j] = c |
| 270 | + return ans |
| 271 | + } |
| 272 | +
|
| 273 | + find := func(word string) bool { |
| 274 | + if !check(word) { |
| 275 | + return false |
| 276 | + } |
| 277 | + for i, b := range board { |
| 278 | + for j, _ := range b { |
| 279 | + if dfs(i, j, 0, word) { |
| 280 | + return true |
| 281 | + } |
| 282 | + } |
| 283 | + } |
| 284 | + return false |
| 285 | + } |
63 | 286 |
|
| 287 | + var ans []string |
| 288 | + for word, _ := range s { |
| 289 | + if find(word) { |
| 290 | + ans = append(ans, word) |
| 291 | + } |
| 292 | + } |
| 293 | + return ans |
| 294 | +} |
64 | 295 | ```
|
65 | 296 |
|
66 | 297 | ### **...**
|
|
0 commit comments