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