自己想的,一个思路两个解法,从字符串中的第一个唯一字符的思路搬过来的
one
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int table2[26]={0};
for(int i=0;i!=magazine.length();++i)
{
table2[magazine[i]-'a']++;
}
for(int i=0;i!=ransomNote.length();++i)
{
table2[ransomNote[i]-'a']--;
if(table2[ransomNote[i]-'a']<0)
{
return false;
}
}
return true;
}
};
two
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int table2[26]={0};
for(char a:magazine)
{
table2[a-'a']++;
}
for(char a:ransomNote)
{
table2[a-'a']--;
if(table2[a-'a']<0)
{
return false;
}
}
return true;
}
};
END