Leetcode 383 Ransom Note
2016-08-31 22:37
363 查看
Given
an
arbitrary
ransom
note
string
and
another
string
containing
lettersfrom
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:
Youmayassumethatbothstringscontainonlylowercaseletters.
Each letter in the magazine string can only be used once in your ransom note.
Note:
Youmayassumethatbothstringscontainonlylowercaseletters.
canConstruct("a","b")->false canConstruct("aa","ab")->false canConstruct("aa","aab")->true 题目大意:给两个string,判断第一个string能否由第二个string里面所含有的字母组成, 第二个string里面的所有字母只能使用一次 分析:建立一个hash数组,对第二个string遍历并记录每个字符出现的次数,然后遍历第一个string, 如果有出现hash里面不存在的字符,那么returnfalse
classSolution{ public: boolcanConstruct(stringransomNote,stringmagazine){ inta[26]={0},lenr=ransomNote.length(),lenm=magazine.length(),i; for(i=0;i<lenm;i++) a[magazine[i]-'a']++; for(i=0;i<lenr;i++) if(a[ransomNote[i]-'a']==0) returnfalse; else a[ransomNote[i]-'a']--; returntrue; } };
相关文章推荐
- [LeetCode-383]Ransom Note(java)
- 【LeetCode】383 Ransom Note(java)
- LeetCode(383)Ransom Note
- LeetCode 383 Ransom Note
- leetcode383: Ransom Note
- LeetCode:383 Ransom Note
- 【LeetCode】383 Ransom Note(java)
- Leetcode 383 Ransom Note
- LeetCode:Ransom Note_383
- 【python】【leetcode】【算法题目383—Ransom Note】
- leetcode 383 Ransom Note
- Java [Leetcode 383]Ransom Note
- 【LeetCode-383】Ransom Note(C++)
- leetcode-383-Ransom Note
- leetcode_c++: Ransom Note(383)
- LeetCode[383] Ransom Note
- LeetCode 383:Ransom Note
- Leetcode:383.Ransom Note(统计每个字符串每个字母的个数)
- LeetCode 383:Ransom Note (c++)
- leetcode383[easy]--- Ransom Note