-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNaiveSuffixArray.cs
More file actions
56 lines (41 loc) · 1.57 KB
/
NaiveSuffixArray.cs
File metadata and controls
56 lines (41 loc) · 1.57 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace AlgorithmsAndDataStructures.DataStructures.SuffixArray;
public class NaiveSuffixArray
{
private readonly string input;
private readonly List<int> suffixes;
public NaiveSuffixArray(string input)
{
suffixes = string.IsNullOrEmpty(input) ? Array.Empty<int>().ToList() : BuildSuffixArray(input);
this.input = input;
}
public IReadOnlyCollection<int> Suffixes => suffixes.ToList().AsReadOnly();
private static List<int> BuildSuffixArray(string input)
{
var suffixArray = new NaiveSuffixArrayNode[input.Length];
for (var i = 0; i < suffixArray.Length; i++)
suffixArray[i] = new NaiveSuffixArrayNode { Suffix = input.Substring(i), Index = i };
suffixArray = suffixArray.OrderBy(arg => arg.Suffix).ToArray();
return suffixArray.Select(arg => arg.Index).ToList();
}
public bool Contains(string pattern)
{
if (string.IsNullOrEmpty(pattern)) return true;
var start = 0;
var end = suffixes.Count - 1;
while (start < end)
{
var mid = start + (end - start) / 2;
var substring = input.Substring(mid, Math.Min(pattern.Length, input.Length - mid));
var comparisonResult = string.Compare(substring, pattern, StringComparison.InvariantCulture);
if (comparisonResult == 0) return true;
if (comparisonResult > 0)
start = mid + 1;
else
end = mid - 1;
}
return false;
}
}