-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch Pattern (Rabin-Karp Algorithm).cpp
More file actions
46 lines (39 loc) · 1.29 KB
/
Search Pattern (Rabin-Karp Algorithm).cpp
File metadata and controls
46 lines (39 loc) · 1.29 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
37
38
39
40
41
42
43
44
45
46
/////GFG
vector <int> search(string pattern, string text)
{
int check=0 , check1=0;
vector<int>ans;
for(int i=0 ; i<pattern.size() ; i++)
{
check+=pattern[i]-'a'+1;
}
int i=0 , j=0;
while(j<text.size())
{
check1+=text[j]-'a' +1;
if(j-i +1 <pattern.size())
{
j++;
}
else if(j-i +1 == pattern.size())
{
if(check==check1)
{
int k2=i , flag=1;
for(int k=0 ; k<pattern.size() ; k++)
{
if(pattern[k]!=text[k2++])
{
flag=0;
break;
}
}
if(flag==1) ans.push_back(i+1);
}
check1-=text[i]-'a'+1;
i++;
j++;
}
}
return ans;
}