This function will take in a body of characters (in the range of a-z, lowercase
ID: 3837048 • Letter: T
Question
This function will take in a body of characters (in the range of a-z, lowercase only) and return a list giving the occurrence count for each string of length 4. For instance, you may have the text "aaaaab" and you would return a vector with 456,976 entries where the first entry is 2 (2 occurrences of aaaa), the second entry is 1 (1 occurrence of aaab), and the remaining entries are all 0. The ordering in the vector should correspond to: Index String 1 aaab 25 aaaz aaba 26 27 aabz 51 52 aaca aacb 53 675 aaZZ 676 abaa 17575 aZZZ 17576 baaa 17577 baab 456974 zzzy 456975 The ordering follows the same as dictionary ordering.Explanation / Answer
#include <iostream>
#include <cstring>
#include <vector>
#include <math.h>
std::vector<unsigned int> countOccurences(const std::string& text)
{
std::vector<unsigned int> v(456976,0);
int len =text.size();
for(int i=0;i<len-3;i++){
string sub=text.substr(i,4);
int ans=0;
for(int j=0;j<4;j++)
{
ans=ans+pow(26,j)*(sub.at(4-j-1)-'a');
}
v[ans]++;
}
return v;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.