forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution2.cs
48 lines (45 loc) · 1.2 KB
/
Solution2.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
public class Solution {
public bool WordBreak(string s, IList<string> wordDict) {
Trie trie = new Trie();
foreach (string w in wordDict) {
trie.Insert(w);
}
int n = s.Length;
bool[] f = new bool[n + 1];
f[n] = true;
for (int i = n - 1; i >= 0; --i) {
Trie node = trie;
for (int j = i; j < n; ++j) {
int k = s[j] - 'a';
if (node.Children[k] == null) {
break;
}
node = node.Children[k];
if (node.IsEnd && f[j + 1]) {
f[i] = true;
break;
}
}
}
return f[0];
}
}
class Trie {
public Trie[] Children { get; set; }
public bool IsEnd { get; set; }
public Trie() {
Children = new Trie[26];
IsEnd = false;
}
public void Insert(string word) {
Trie node = this;
foreach (char c in word) {
int i = c - 'a';
if (node.Children[i] == null) {
node.Children[i] = new Trie();
}
node = node.Children[i];
}
node.IsEnd = true;
}
}