This repository was archived by the owner on Oct 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount Occurences of Anagrams.cpp
More file actions
69 lines (63 loc) ยท 1.43 KB
/
Count Occurences of Anagrams.cpp
File metadata and controls
69 lines (63 loc) ยท 1.43 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
67
68
69
//https://practice.geeksforgeeks.org/problems/count-occurences-of-anagrams5839/1
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution
{
public:
int search(string pat, string txt)
{
// code here
long long int k = pat.size();
long long int n = txt.size();
long long int fre1[26]={0};
long long int fre2[26]={0};
for(auto c:pat)fre1[c-'a']++;
long long int i = 0;
long long int j = 0;
long long res = 0;
while(j<n)
{
fre2[txt[j]-'a']++;
if(j<k-1)
{
j++;
}
else
{
bool flag=true;
for(int it=0; it<26; it++)
{
if(fre1[it]!=fre2[it])
{
flag= false;
break;
}
}
if(flag)res++;
fre2[txt[i]-'a']--;
i++;
j++;
}
}
return res;
}
};
//{ Driver Code Starts.
int main()
{
int t =1;
// cin >> t;
while (t--)
{
string pat="for", txt="forxxorfxdofr";
// cin >> txt >> pat;
Solution ob;
auto ans = ob.search(pat, txt);
cout << ans << "\n";
}
return 0;
}
// } Driver Code Ends