Skip to content
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
arst
algorithms-and-data-structures
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
algorithms-and-data-structures
/
AlgorithmsAndDataStructures
/
Algorithms
/
Strings
/
Search
/
NaivePatternSearch.cs
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
35 lines (27 loc) · 994 Bytes
master
Breadcrumbs
algorithms-and-data-structures
/
AlgorithmsAndDataStructures
/
Algorithms
/
Strings
/
Search
/
NaivePatternSearch.cs
Copy path
Top
File metadata and controls
Code
Blame
35 lines (27 loc) · 994 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
namespace AlgorithmsAndDataStructures.Algorithms.Strings.Search;
public class NaivePatternSearch : IStringPatternSearchAlgorithm
{
public int Search(string input, string pattern)
{
if (string.IsNullOrEmpty(input) || string.IsNullOrEmpty(pattern)) return -1;
const int negativeResult = -1;
if (pattern.Length > input.Length) return negativeResult;
for (var i = 0; i <= input.Length - pattern.Length; i++)
if (input[i] == pattern[0])
{
var originalPosition = i + 1;
var isMatch = true;
for (var j = 1; j < pattern.Length; j++)
{
if (input[originalPosition] != pattern[j])
{
isMatch = false;
break;
}
originalPosition++;
}
if (isMatch) return i;
}
return negativeResult;
}
}
You can’t perform that action at this time.