-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy path0140-WordBreakII.cs
46 lines (39 loc) · 1.34 KB
/
0140-WordBreakII.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
//-----------------------------------------------------------------------------
// Runtime: 256ms
// Memory Usage: 33.3 MB
// Link: https://leetcode.com/submissions/detail/373697376/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _0140_WordBreakII
{
public IList<string> WordBreak(string s, IList<string> wordDict)
{
var set = new HashSet<string>(wordDict);
var cache = new Dictionary<string, IList<string>>();
return DFS(s, set, cache);
}
public IList<string> DFS(string s, HashSet<string> set, Dictionary<string, IList<string>> cache)
{
if (cache.ContainsKey(s)) return cache[s];
var result = new List<string>();
if (set.Contains(s))
{
result.Add(s);
}
for (int i = 1; i < s.Length; i++)
{
var str = s.Substring(i);
if (set.Contains(str))
{
var prevResults = DFS(s.Substring(0, i), set, cache);
foreach (var prev in prevResults)
result.Add(prev + " " + str);
}
}
cache[s] = result;
return result;
}
}
}