Given a string S and a word C, return the count of the occurrences of anagrams of the word in the text. Both string and word are in lowercase letter
belongs to collection: Interview C++ coding problems/challenges | String
All Answers
total answers (1)

C++ programming
Algorithm:
1. Set count=0; For i=0:i<a.length()-b.length()+1 IF (check(a.substr(i,b.length()),b)) count++; 2. Print countSo the idea is pretty simple. What we are doing is to slide the window and check for anagrams. Window is nothing but the substring being extracted every time. Window length is equal to the word length.
To check anagrams
FUNCTION check(string a, string b) 1. Declare a map <char, int="" style="box-sizing: border-box;"> m; //to store frequency of each character 2. For string abuild the map For (int i=0;i<a.length();i++) m[a[i]]++; END FOR 3. For string bdestruct the map. For (int i=0;i<b.length();i++) m[b[i]]--; END FOR 4. If map is totally destructed that means all key has value perfectly 0 return true; Else Return false. END FUNCTION </char,>So this basically means, we are constructing a container using the elements of string a. Then we are destructing the map to form string b. If such formation is possible they strings are anagrams of each other.
C++ implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer