forked from sureshmangs/Code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_occurence_of_anagram.cpp
81 lines (64 loc) · 1.53 KB
/
count_occurence_of_anagram.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
71
72
73
74
75
76
77
78
79
80
81
/*
Given a word pat and a text txt.
Return the count of the occurences of anagrams of the word in the text.
Example 1:
Input:
txt = forxxorfxdofr
pat = for
Output: 3
Explanation: for, orf and ofr appears
in the txt, hence answer is 3.
Example 2:
Input:
txt = aabaabaa
pat = aaba
Output: 4
Explanation: aaba is present 4 times
in txt.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(26) or O(256)
Constraints:
1 <= |pat| <= |txt| <= 105
Both string contains lowercase english letters.
*/
//User function template for C++
class Solution{
public:
int search(string pat, string txt) {
int start = 0, end = 0, res = 0, match = 0, k = pat.length();
unordered_map <char, int> m;
for (auto &x: pat) m[x]++;
while (end < txt.length()) {
char right = txt[end];
if (m.find(right) != m.end()) {
m[right]--;
if (m[right] == 0) match++;
if (match == m.size()) res++;
}
if (end - start + 1 < k) end++;
else if (end - start + 1 == k) {
char left = txt[start];
if (m.find(left) != m.end()) {
if (m[left] == 0) match--;
m[left]++;
}
start++;
end++;
}
}
return res;
}
};
// { Driver Code Starts.
int main() {
int t;
cin >> t;
while (t--) {
string pat, txt;
cin >> txt >> pat;
Solution ob;
auto ans = ob.search(pat, txt);
cout << ans << "\n";
}
return 0;
} // } Driver Code Ends