17.9 Design a method to find the frequency of occurrences of any given word in a book.
This problem lets us find the frequency of the word appearing in the book, so the first thing we need to make clear is that we only need to count one word or multiple words. If it is a word, then directly traverse all the words directly statistics can, if there are multiple, you need to establish a hash table to establish the mapping between each word and its occurrence, and then to find it, see the code is as follows:
unordered_map<string,int> Make_dictionary (vector<string>Book ) {Unordered_map<string,int>Res; for(Auto Word:book) { for(Auto &a:word) A =ToLower (a); ++Res[word]; } returnRes;}intGet_frequency (unordered_map<string,int> m,stringword) { if(M.empty () | | word.empty ())return-1; for(Auto &a:word) A =ToLower (a); returnM[word];}
Careercup all in one topic summary
[Careercup] 17.9 word Frequency in a book word frequency