跳至主要内容

383. Ransom Note

class Solution {
public:
bool canConstruct(string ransomNote, string magazine)
{
vector<int> freq(26);

for(auto& c : magazine) ++freq[ c - 'a'];

for(int i = 0; i < ransomNote.size(); i++)
{
int c = ransomNote[i] - 'a';
if (--freq[c] < 0) return false;
}
return true;
}
};
  • T: O(N)O(N)
  • S: O(1)O(1)