-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cs
More file actions
72 lines (65 loc) · 1.71 KB
/
Solution.cs
File metadata and controls
72 lines (65 loc) · 1.71 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
public class Solution
{
public IList<string> FindWords(char[][] board, string[] words)
{
int m = board.Length, n = board[0].Length;
Trie trie = new Trie();
foreach (var word in words)
{
trie.Insert(word);
}
List<string> ans = [];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
DFS(i, j, board, trie, ans);
}
}
return ans;
}
private void DFS(int x, int y, char[][] board, Trie trie, List<string> ans)
{
int m = board.Length, n = board[0].Length;
if (x < 0 || y < 0 || x >= m || y >= n) return;
char c = board[x][y];
if (c == '#' || trie.Next[c - 'a'] == null) return;
trie = trie.Next[c - 'a'];
if (trie.Word is not null)
{
ans.Add(trie.Word);
trie.Word = null;
}
board[x][y] = '#';
DFS(x + 1, y, board, trie, ans);
DFS(x - 1, y, board, trie, ans);
DFS(x, y + 1, board, trie, ans);
DFS(x, y - 1, board, trie, ans);
board[x][y] = c;
}
}
public class Trie
{
public string Word { get; set; }
public Trie[] Next { get; set; }
public Trie()
{
Next = new Trie[26];
Word = null;
}
public void Insert(string word)
{
if (string.IsNullOrWhiteSpace(word)) return;
Trie curr = this;
for (int i = 0; i < word.Length; i++)
{
int index = word[i] - 'a';
if (curr.Next[index] is null)
{
curr.Next[index] = new Trie();
}
curr = curr.Next[index];
}
curr.Word = word;
}
}