哈希表解题
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
canConstruct(“a”, “b”) -> false
canConstruct(“aa”, “ab”) -> false
canConstruct(“aa”, “aab”) -> true
其实这一题蛮简单的,就是一个哈希表的简单应用:
public boolean canConstruct(String ransomNote, String magazine) {
int nums1[]=new int[26],nums2[]=new int[26],i;
char ch1[]=ransomNote.toCharArray(),ch2[]=magazine.toCharArray();
for(char x:ch1)
nums1[x-'a']++;
for(char x:ch2)
nums2[x-'a']++;
for(i=0;i<26;i++)
if(nums1[i]>nums2[i])
return false;
return true;
}