- Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathNaiveStringSearch.cs
42 lines (38 loc) · 1.31 KB
/
NaiveStringSearch.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
usingSystem.Collections.Generic;
// Implements the traditional naive string matching algorithm in C# for TheAlgorithms/C-Sharp.
namespaceAlgorithms.Strings.PatternMatching;
/// <summary>
/// Implements the traditional naive string matching algorithm in C#.
/// </summary>
publicstaticclassNaiveStringSearch
{
/// <summary>
/// NaiveSearch(Content, Pattern) will return an array containing each index of Content in which Pattern appears.
/// Cost: O(n*m).
/// </summary>
/// <param name="content">The text body across which to search for a given pattern.</param>
/// <param name="pattern">The pattern against which to check the given text body.</param>
/// <returns>Array containing each index of Content in which Pattern appears.</returns>
publicstaticint[]NaiveSearch(stringcontent,stringpattern)
{
varm=pattern.Length;
varn=content.Length;
List<int>indices=new();
for(vare=0;e<=n-m;e++)
{
intj;
for(j=0;j<m;j++)
{
if(content[e+j]!=pattern[j])
{
break;
}
}
if(j==m)
{
indices.Add(e);
}
}
returnindices.ToArray();
}
}