-
Notifications
You must be signed in to change notification settings - Fork 112
/
1065-IndexPairsOfAString.cs
67 lines (57 loc) · 1.78 KB
/
1065-IndexPairsOfAString.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
//-----------------------------------------------------------------------------
// Runtime: 240ms
// Memory Usage: 32.4 MB
// Link: https://leetcode.com/submissions/detail/336996248/
//-----------------------------------------------------------------------------
using System.Collections.Generic;
namespace LeetCode
{
public class _1065_IndexPairsOfAString
{
public int[][] IndexPairs(string text, string[] words)
{
var trie = new Trie();
foreach (var word in words)
{
var current = trie;
foreach (var ch in word)
{
if (current[ch] == null)
current[ch] = new Trie();
current = current[ch];
}
current.End = true;
}
var result = new List<int[]>();
for (int left = 0; left < text.Length; left++)
{
var current = trie;
for (int right = left; right < text.Length; right++)
{
var ch = text[right];
if (current[ch] == null)
break;
current = current[ch];
if (current.End)
result.Add(new int[] { left, right });
}
}
return result.ToArray();
}
}
public class Trie
{
private IDictionary<char, Trie> list = new Dictionary<char, Trie>();
public Trie this[char key]
{
get
{
if (!list.ContainsKey(key))
return null;
return list[key];
}
set { list[key] = value; }
}
public bool End { get; set; }
}
}