[Careercup] 17.9 word Frequency in a book word frequency

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.