mirror of
https://github.com/ShusenTang/LeetCode.git
synced 2024-09-02 14:20:01 +00:00
17 lines
536 B
Markdown
17 lines
536 B
Markdown
![]() |
# [383. Ransom Note](https://leetcode.com/problems/ransom-note/description/)
|
|||
|
# 思路
|
|||
|
用一个大小为26的数组count记录magazine中26个字母的出现次数,只要每个字母出现次数不小于ransomNote对应字母次数就行了。
|
|||
|
# 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;
|
|||
|
}
|
|||
|
};
|
|||
|
```
|