-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRabinKarpStringMatchingHashTechnique.cs
More file actions
36 lines (33 loc) · 1.27 KB
/
RabinKarpStringMatchingHashTechnique.cs
File metadata and controls
36 lines (33 loc) · 1.27 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DynamicProgrammingAlgorithms
{
class RabinKarpStringMatchingHashTechnique
{
// the method made use of hashing technique
public bool RabinCarpMatchingCheck(string pattern, StringBuilder sampleString, bool caseInsensitive = false)
{
// if case insensitiveness is desired or not
if (caseInsensitive)
{
pattern = pattern.ToLower();
sampleString = sampleString.Replace(sampleString.ToString(), sampleString.ToString().ToLower());
}
int N = pattern.Length;
//traverse the sampleString
for (int i = 0; i < sampleString.Length; i++)
{
//if i+N reaches the max limit then break the loop, it simlpy means there is no match
if (i + N > sampleString.Length)
break;
//compare hash codes of substrings which are size of N
if (pattern.GetHashCode() == sampleString.ToString().Substring(i, N).GetHashCode())
return true;
}
return false;
}
}
}