Skip to content

Commit

Permalink
Added file for count occurences of anagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
Spart-v6 authored Oct 2, 2021
1 parent acb6314 commit 86ee8fa
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions Count_Occurences_of_Anagrams.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include<bits/stdc++.h>
using namespace std;
#define IO() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

int main(){

IO();

string s = "forxxorfxdofr";
string anagram = "for";
int k = anagram.size();

unordered_map<char,int> m;
for(auto &i: anagram){
m[i]++;
}
int count = m.size();

// vector<int> v;

int i = 0, j = 0, ans = 0;
while(j < s.size()){
// calculations
if(m.find(s[j]) != m.end()){
m[s[j]]--;
if(m[s[j]] == 0) count--;
}

if(j - i + 1 < k ) j++;

else if(j - i + 1 == k){
// take answer from calculations
if(count == 0) {
ans++;
// v.push_back(i);
}

if(m.find(s[i]) != m.end()){
m[s[i]]++;
if(m[s[i]] == 1) count++;
}
//slide the window
i++; j++;
}
}
cout<<ans;
// for(auto i : v){
// cout << i << " ";
// }


return 0;
}

0 comments on commit 86ee8fa

Please sign in to comment.