The road of leetcode cultivation--383. Ransom Note

Source: Internet
Author: User
Tags first string

The title is like this.

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

Title Analysis: In the second string, whether to include all the characters that appear in the first string (the number of occurrences of the repeating count), each character in the second string can be used one at a time.

It's quite simple to remember: the number of occurrences of each character in the first string is compared to the number of occurrences of each string in the second string.

Learn from other people's ideas (lowercase letters only):
 Public Static Booleancanconstruct (String ransomnote, String magazine) {if(Ransomnote.length () >magazine.length ()) {            return false; }        int[] A =New int[26];//a maximum of 26 letters        int[] B =New int[26];  for(inti = 0; I < ransomnote.length (); i++) {A[ransomnote.charat (i)-' a ']++;//The number of characters to judge, if already exist, on + + (the most important here)        }         for(inti = 0; I < magazine.length (); i++) {B[magazine.charat (i)-' A ']++; }         for(inti = 0; i < a.length; i++) {            if(A[i] > B[i]) {//determines whether each of the first array has a value greater than the second array                return false; }        }        return true; }

The code is very simple, the key is the idea! Ideas! Ideas!

Important thing to say three times haha ^_^

The road of leetcode cultivation--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.