-
Notifications
You must be signed in to change notification settings - Fork 0
/
StringMatching.cpp
42 lines (37 loc) · 994 Bytes
/
StringMatching.cpp
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
#include<bits/stdc++.h>
using namespace std;
// class Solution
// {
// public:
vector <int> search(string pattern, string text)
{
vector<int> ans;
int n = text.size();
int m = pattern.size();
for(int i=0; i<=n-m; i++){
// bool flag = true;
int j;
for(j=0; j<m; j++)
if(text[i+j] != pattern[j]){
// flag = false;
break;
}
// if(flag == true)
if(j==m)
ans.push_back(i+1);
}
return ans;
}
// TC: O(n*m);
// In case of Rabin CARP Algorithm - O(n + m); by Hashing .................
int main(){
string text,pattern;
text = "asdfgasdf";
pattern = "asdf";
vector<int>vec;
vec = search(pattern,text);
for (int i=0;i<vec.size();i++){
cout<<vec[i]<<" ";
}
return 0;
}