您的位置:首页 > 其它

leetcode 383. Ransom Note

2016-08-20 12:30 351 查看
<span style="font-family: Arial, Helvetica, sans-serif;">public:</span>
bool canConstruct(string ransomNote, string magazine) {
map<char,int> m;
int len1=ransomNote.size();
int len2=magazine.size();
int icount=0;
int fd=0;
if(len1>len2){return false;}
else
{
for (int i=0;i<len2;i++){
m[magazine[i]]++;
}
for (int j=0;j<len1;j++){
icount=m[ransomNote[j]];
if(icount!=0){
icount--;
m[ransomNote[j]]=icount;
fd++;
}
else
{ return false;}
}
if(fd==len1)
{return true;}
else return false;

}

}
};



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.
canConstruct("a", "b") -> false
canConstruct("aa", "ab") -> false
canConstruct("aa", "aab") -> true
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: