-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.cs
29 lines (28 loc) · 891 Bytes
/
Solution.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
using System.Collections.Generic;
using System.Linq;
public class Solution {
public bool WordBreak(string s, IList<string> wordDict) {
var f = new bool[s.Length + 1];
f[0] = true;
var wordDictGroup = wordDict.GroupBy(word => word.Length);
for (var i = 1; i <= s.Length; ++i)
{
foreach (var wordGroup in wordDictGroup)
{
var wordLength = wordGroup.Key;
if (i >= wordLength && f[i - wordLength])
{
foreach (var word in wordGroup)
{
if (s.Substring(i - wordLength, wordLength) == word)
{
f[i] = true;
break;
}
}
}
}
}
return f[s.Length];
}
}