-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagramPattern.cpp
More file actions
44 lines (38 loc) · 972 Bytes
/
anagramPattern.cpp
File metadata and controls
44 lines (38 loc) · 972 Bytes
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
#include<bits/stdc++.h>
using namespace std;
// linearithmic time complexity --> O(n)
// search pattern and anagram in string
// geeksforgeeks
// egek --> 1
bool same(vector<int> a, vector<int> b){
for(int i=0;i<a.size();i++){
if(a[i] != b[i]){
return false;
}
}
return true;
}
bool check(string s1, string s2){
vector<int> countText(256);
vector<int> countPat(256);
// window creation
for(int i=0;i<s2.size();i++){
countText[s1[i]]++;
countPat[s2[i]]++;
}
// checking window of size 4
for(int i=s2.size();i<s1.size();i++){
if(same(countText, countPat)){
return true;
}
countText[s1[i]]++; // adding another char to the window
countText[s1[i - s2.size()]]--; // removing start char from the window
}
return false;
}
int main() {
string s1 = "geeksforgeeks";
string s2 = "egek";
cout << check(s1, s2);
return 0;
}