diff --git a/AhoCorasick/AhoCorasick.cs b/AhoCorasick/AhoCorasick.cs
index dbe6fa2..53a6653 100644
--- a/AhoCorasick/AhoCorasick.cs
+++ b/AhoCorasick/AhoCorasick.cs
@@ -132,7 +132,7 @@ public void BuildFail(Trie node = null)
var word = node.Word;
for (int i = 1; i < word.Length && node.Fail == null; i++)
- node.Fail = Trie.ExploreFailLink(word.Substring(i));
+ node.Fail = Trie.ExploreFailLink(word, i, word.Length);
foreach (var subNode in node.Next.Values)
BuildFail(subNode);
diff --git a/AhoCorasick/Trie.cs b/AhoCorasick/Trie.cs
index b152296..710550e 100644
--- a/AhoCorasick/Trie.cs
+++ b/AhoCorasick/Trie.cs
@@ -87,18 +87,23 @@ public virtual Trie Add(string word)
}
///
- /// Finds the failure node for a specified suffix.
+ /// Finds the failure node for a specified suffix within the given range of indices.
///
- /// The suffix.
- /// The failure node or null.
- public virtual Trie ExploreFailLink(string word)
+ /// The string containing the suffix.
+ /// The start index of the suffix within the string.
+ /// The end index (exclusive) of the suffix within the string.
+ /// The failure node or null if no failure node is found.
+
+ public virtual Trie ExploreFailLink(string word, int startIndex, int endIndex)
{
var node = this;
- foreach (var c in word)
+ for (int i = startIndex; i < endIndex; i++)
{
- node.Next.TryGetValue(c, out node);
- if (node == null) return null;
+ if (!node.Next.TryGetValue(word[i], out node))
+ {
+ return null;
+ }
}
return node;