Java [Leetcode 383]ransom Note

Source: Internet
Author: User

Title Description:

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

Problem Solving Ideas:

Open 26 arrays to deposit the number of magzine letters, each position the number of corresponding letters;

For the ransom string, each letter is read to reduce the number of letters in the corresponding position by one, if the number of letters is less than 0, it indicates that the letter is not sufficient to return false, otherwise it returns true.

The code is as follows:

public class Solution {public    Boolean canconstruct (String Ransomnote, String magazine) {    int[] Ran_array = new in T[26];    for (int i = 0; i < magazine.length (); i++) {    Ran_array[magazine.charat (i)-' a ']++;    }    for (int i = 0; i < ransomnote.length (); i++) {    if (--ran_array[ransomnote.charat (i)-' a '] < 0)    return false;< c8/>}    return true;}            }

  

Java [Leetcode 383]ransom Note

Related Article

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.