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