LeetCode 383. Ransom Note
2017-04-04 08:16
323 查看
Description
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
Analysis
统计magazine中各小写字母出现的次数,然后供
ransomNote使用,若发现某字母不足,则返回
false;函数未在循环中途
return,则说明为
true。
Code
class Solution { public: bool canConstruct(string ransomNote, string magazine) { int maga[26] = {0}; for (char c : magazine) maga[c- 97]++; for (char c : ransomNote) if (--maga[c - 97] < 0) return false; return true; } };
Appendix
Link: https://leetcode.com/problems/ransom-note/Run Time: 19ms, beats 97.57%
相关文章推荐
- LeetCode 383. Ransom Note
- LeetCode 383. Ransom Note
- Leetcode 383. Ransom Note
- LeetCode 383. Ransom Note
- 【leetcode】383. Ransom Note 恐吓信
- 383. Ransom Note [LeetCode]
- LeetCode | 383. Ransom Note
- leetcode(383. Ransom Note)
- Leetcode——383. Ransom Note
- leetcode(85).383. Ransom Note
- leetCode 383. Ransom Note 字符串
- Leetcode 383. Ransom Note 构造字符串 解题报告
- leetcode_383. Ransom Note-近似子串问题
- leetcode 383. Ransom Note 勒索信
- LeetCode之383. Ransom Note
- LeetCode 383. Ransom Note
- LeetCode-383. Ransom Note
- leetcode题解-58. Length of Last Word && 67. Add Binary && 383. Ransom Note
- LeetCode 383. Ransom Note
- LeetCode 383. Ransom Note 解题报告