🎉 One-stop destination for all your technical interview Preparation 🎉
Given two stings ransomNote and magazine, return true if ransomNote can be constructed from magazine and false otherwise.
Each letter in magazine can only be used once in ransomNote
class Solution{
public:
bool canConstruct(string ransomNote, string magazine) {
vector<int> a(26, 0);
for (auto &x : magazine)
a[x - 'a']++;
for (auto &x : ransomNote) {
if (--a[x - 'a'] < 0)
return false;
}
return true;
}
};