<leetcode oj> 383. Ransom Note

Source: Internet
Author: User

Given? An? arbitrary? Ransom? Note? String another? containing? Letters from? All magazines? Write a? function? That would return? True if? Ransom? Note: Can constructed? from the magazines; otherwise,? It--it'll return false. ??

Each letter? In? The? Magazine? string? can? Only is? Used? Once? In? Your? Ransom? Note.

Note:
You may assume this both strings contain only lowercase letters.

Canconstruct ("A", "B")-Falsecanconstruct ("AA", "AB"), falsecanconstruct ("AA", "AaB"), True

Analysis:

Determines whether a given string ransom can be generated by a combination of some characters in a character in another string magazine.

Obviously, count the number of characters to appear!

Class Solution {public:    bool Canconstruct (String Ransomnote, String magazine) {        vector<int> charcnt (26,0 );        Count the occurrences of each character in the magazine for        (int i=0;i<magazine.size (); i++)            charcnt[magazine[i]-' a ']++;        Count the occurrences of each character in the ransomnote for            (int i=0;i<ransomnote.size (); i++)            charcnt[ransomnote[i]-' a ']--;        Check if the number in the Ransomnote is less than magazine! for        (int i=0;i<ransomnote.size (); i++)            if (charcnt[ransomnote[i]-' a '] < 0)                return false;        return true;    }};


Note: This blog post is Ebowtang original and may continue to be updated later in this article. If reproduced, please make sure to copy this article information!

Original address: http://blog.csdn.net/ebowtang/article/details/52187660

Original Author Blog: Http://blog.csdn.net/ebowtang

This blog leetcode key index: http://blog.csdn.net/ebowtang/article/details/50668895

<leetcode oj> 383. Ransom Note

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.