-
Notifications
You must be signed in to change notification settings - Fork 0
/
081_FindPatternInString.cpp
70 lines (57 loc) · 1.33 KB
/
081_FindPatternInString.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <bits/stdc++.h>
using namespace std;
int LCS(string &X, string &Y){
int m=X.length();
int n=Y.length();
int t[m+1][n+1]={0};
int result=INT_MIN;
for(int i=0; i<=m; i++){
for(int j=0; j<=n; j++){
if(i==0 || j==0){
t[i][j]=0;
}else{
if(X[i-1]==Y[j-1]){
t[i][j]=1+t[i-1][j-1];
// result=max(result, t[i][j]);
if(t[i][j]>result){
result=t[i][j];
}
}else{
t[i][j]=0;
}
}
}
}
return result==INT_MIN?0:result;
}
bool findPattern(string p, string s)
{
// Write your code here.
int m=p.length();
int n=s.length();
if(m>n) return false;
// method-1
// return m==LCS(p, s);
// method-2
// for(int i=0; i<n; i++){
// bool flag=true;
// for(int j=0; j<m; j++){
// if(p[j]!=s[i+j]){
// flag=false;
// break;
// }
// }
// if(flag){
// return true;
// }
// }
// return false;
// method-3
for(int i=0; i<n-m+1; i++){
string str=s.substr(i, m);
if(str==p){
return true;
}
}
return false;
}