<LeetCode OJ> 383. Ransom Note
2016-08-11 23:36
435 查看
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.
分析:DONE
判断给定字符串Ransom 能否由另一字符串magazine中字符中的某些字符组合生成。
显然,统计字符出现次数即可!
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/52187660
原作者博客:http://blog.csdn.net/ebowtang
本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895
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
分析:DONE
判断给定字符串Ransom 能否由另一字符串magazine中字符中的某些字符组合生成。
显然,统计字符出现次数即可!
class Solution { public: bool canConstruct(string ransomNote, string magazine) { vector<int> charcnt(26,0); //统计magazine中每个字符出现次数 for(int i=0;i<magazine.size();i++) charcnt[magazine[i]-'a']++; //统计ransomNote中每个字符出现次数 for(int i=0;i<ransomNote.size();i++) charcnt[ransomNote[i]-'a']--; //检查是否ransomNote中的数量是否都小于magazine中的! for(int i=0;i<ransomNote.size();i++) if(charcnt[ransomNote[i]-'a'] < 0) return false; return true; } };
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/52187660
原作者博客:http://blog.csdn.net/ebowtang
本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895
相关文章推荐
- <LeetCode OJ> 13 / 12 Roman to Integer & Integer to Roman
- <LeetCode OJ> 8. String to Integer (atoi)
- <LeetCode OJ> 389. Find the Difference
- <LeetCode OJ> 67. Add Binary
- <LeetCode OJ> 9. Palindrome Number
- <LeetCode OJ> (104 / 111) Maximum & Minimum Depth of Binary Tree
- <LeetCode OJ> 303. Range Sum Query - Immutable
- <LeetCode OJ> (198 / 213) House Robber(I / II)
- <LeetCode OJ> 231 / 342 Power of (Two / Four)
- <LeetCode OJ> 382. Linked List Random Node
- <LeetCode OJ> 70. Climbing Stairs
- <LeetCode OJ> 66. Plus One
- <LeetCode OJ> 21. Merge Two Sorted Lists
- <> reading note 2011-4-6 21:57:08
- <LeetCode OJ> 19. Remove Nth Node From End of List
- <LeetCode OJ> 148. Sort List
- <C/C++算法>九度OJ题目1201--1358解题练习(六)
- <LeetCode OJ> 191. Number of 1 Bits
- <九度 OJ>题目1084:整数拆分
- <C/C++算法>九度OJ题目1151--1200解题练习(五)