using
System;
using
System.Collections.Generic;
class
GFG {
static
Dictionary<
char
, List<
int
> > structuredText
=
new
Dictionary<
char
, List<
int
> >();
static
void
StringSearch(
string
st)
{
for
(
int
i = 0; i < st.Length; i++) {
char
ch = st[i];
if
(structuredText.ContainsKey(ch)) {
structuredText[ch].Add(i);
}
else
{
structuredText.Add(ch,
new
List<
int
>{ i });
}
}
}
static
void
pattern_search(
string
st,
string
pattern)
{
StringSearch(st);
Queue<
int
> q_indices =
new
Queue<
int
>();
if
(structuredText.ContainsKey(pattern[0])) {
q_indices =
new
Queue<
int
>(
structuredText[pattern[0]]);
}
int
pat_len = pattern.Length;
for
(
int
i = 1; i < pat_len; i++) {
char
ch = pattern[i];
int
q_size = q_indices.Count;
while
(q_size-- > 0) {
int
ind = q_indices.Dequeue();
if
(structuredText.ContainsKey(ch)
&& structuredText[ch].Contains(ind
+ 1)) {
q_indices.Enqueue(ind + 1);
}
}
}
Console.Write(
"Pattern found at indexes:"
);
while
(q_indices.Count > 0) {
int
last_ind = q_indices.Dequeue();
Console.Write(
" "
+ (last_ind - (pat_len - 1)));
}
Console.WriteLine();
}
static
void
Main(
string
[] args)
{
string
text =
"Welcome to Geeks for Geeks"
;
string
pattern =
"Geeks"
;
pattern_search(text, pattern);
}
}