0%

383. Ransom Note

O(m+n) time

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
int freq[26] = {0};
for (char c : magazine) {
++freq[c - 'a'];
}
for (char c : ransomNote) {
if (--freq[c - 'a'] < 0) return false;
}
return true;
}
};