leetcode 383. Ransom Note
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
AC:
bool canConstruct(char* ransomNote, char* magazine) {
int len1=strlen(ransomNote);
int len2=strlen(magazine);
int a[26]={0};
int b[26]={0};
for(int i=0;i<len1;i++)
{
a[ransomNote[i]-'a']++;
}
for(int i=0;i<len2;i++)
{
b[magazine[i]-'a']++;
}
for(int i=0;i<26;i++)
{
if(b[i]<a[i])
{
return false;
}
}
return true;
}