LeetCode/solutions/383. Ransom Note.md
2019-09-13 23:08:41 +08:00

17 lines
539 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# [383. Ransom Note](https://leetcode.com/problems/ransom-note/description/)
# 思路
用一个大小为26的数组count记录magazine中26个字母的出现次数只要每个字母出现次数不小于ransomNote对应字母次数就行了。
# C++
```C++
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
vector<int>count(26);
for(char c: magazine) count[c - 'a']++;
for(char c: ransomNote)
if(0 > --count[c - 'a']) return false;
return true;
}
};
```