-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0425-WordSquares.cs
88 lines (76 loc) · 2.6 KB
/
0425-WordSquares.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
//-----------------------------------------------------------------------------
// Runtime: 340ms
// Memory Usage: 38.4 MB
// Link: https://leetcode.com/submissions/detail/409243426/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
using System.Text;
namespace LeetCode
{
public class _0425_WordSquares
{
public IList<IList<string>> WordSquares(string[] words)
{
var root = BuildTrie(words);
var results = new List<IList<string>>();
foreach (var word in words)
{
var square = new List<string>();
square.Add(word);
BackTracking(1, words, root, square, results);
}
return results;
}
private void BackTracking(int step, string[] words, Trie root, List<string> square, IList<IList<string>> results)
{
if (step == words[0].Length)
{
results.Add(new List<string>(square));
return;
}
var sb = new StringBuilder();
foreach (var word in square)
sb.Append(word[step]);
foreach (var index in GetWordsWithPrefix(sb.ToString(), root))
{
square.Add(words[index]);
BackTracking(step + 1, words, root, square, results);
square.RemoveAt(square.Count - 1);
}
}
private Trie BuildTrie(string[] words)
{
var root = new Trie();
for (int i = 0; i < words.Length; i++)
{
var word = words[i];
var current = root;
foreach (var ch in word)
{
if (current.Children[ch - 'a'] == null)
current.Children[ch - 'a'] = new Trie();
current = current.Children[ch - 'a'];
current.WordIndexes.Add(i);
}
}
return root;
}
private IList<int> GetWordsWithPrefix(string prefix, Trie root)
{
var current = root;
foreach (var ch in prefix)
{
if (current.Children[ch - 'a'] == null)
return new List<int>();
else
current = current.Children[ch - 'a'];
}
return current.WordIndexes;
}
public class Trie
{
public Trie[] Children { get; } = new Trie[26];
public IList<int> WordIndexes = new List<int>();
}
}
}