-
Notifications
You must be signed in to change notification settings - Fork 112
/
0212-WordSearchII.cs
73 lines (63 loc) · 2.37 KB
/
0212-WordSearchII.cs
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
//-----------------------------------------------------------------------------
// Runtime: 272ms
// Memory Usage: 48.6 MB
// Link: https://leetcode.com/submissions/detail/380046401/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0212_WordSearchII
{
public IList<string> FindWords(char[][] board, string[] words)
{
var result = new List<string>();
var root = BuildTrie(words);
var N = board.Length;
var M = board[0].Length;
for (int i = 0; i < N; i++)
for (int j = 0; j < M; j++)
BackTracking(board, i, j, N, M, root, result);
return result;
}
private void BackTracking(char[][] board, int i, int j, int N, int M, TrieNode node, IList<string> results)
{
var ch = board[i][j];
if (ch == '#' || node.Children[ch - 'a'] == null) return;
node = node.Children[ch - 'a'];
if (node.IsFinished)
{
results.Add(node.Word);
node.IsFinished = false;
}
board[i][j] = '#';
if (i > 0) BackTracking(board, i - 1, j, N, M, node, results);
if (j > 0) BackTracking(board, i, j - 1, N, M, node, results);
if (i < N - 1) BackTracking(board, i + 1, j, N, M, node, results);
if (j < M - 1) BackTracking(board, i, j + 1, N, M, node, results);
board[i][j] = ch;
}
private TrieNode BuildTrie(string[] words)
{
var root = new TrieNode();
foreach (var word in words)
{
var currentNode = root;
foreach (var ch in word)
{
if (currentNode.Children[ch - 'a'] == null)
currentNode.Children[ch - 'a'] = new TrieNode();
currentNode = currentNode.Children[ch - 'a'];
}
currentNode.IsFinished = true;
currentNode.Word = word;
}
return root;
}
public class TrieNode
{
public TrieNode[] Children { get; } = new TrieNode[26];
public bool IsFinished { get; set; }
public string Word { get; set; }
}
}
}