-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathPatternSearch.cpp
More file actions
66 lines (50 loc) · 1.35 KB
/
PatternSearch.cpp
File metadata and controls
66 lines (50 loc) · 1.35 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
//Function to check if the given pattern exists in the given string or not.
bool search(string pat, string txt)
{
int M = pat.length();
int N = txt.length();
int i = 0;
//iterating over given text to search for pattern.
while (i <= N - M)
{
int j;
//checking for pattern from current index i in the text.
//if any character, differs we break the loop.
for (j = 0; j < M; j++)
if (txt[i + j] != pat[j])
break;
//if loop is not broken it means that we found the
//pattern so we return true.
if (j == M)
{
return true;
}
//else sliding the pointer by j indexes (or by 1 if j=0).
else if (j == 0)
i = i + 1;
else
i = i + j;
}
//returning false if pattern is not found.
return false;
}
};
// { Driver Code Starts.
// Driver Code
int main()
{
int t;
cin >> t;
while(t--){
string s, p;
cin >> s >> p;
Solution obj;
if(obj.search(p, s)) cout << "Yes"; else cout << "No"; cout << endl;
}
return 0;
}