https://leetcode.cn/problems/ransom-note/description/

<aside> 💡

242.有效的字母异位词几乎完全一样(冒充新题)

242. 有效的字母异位词

不同点就是两个字符串不需要组成元素完全相同,只需要magazine能够完全包含ransomNote

</aside>

bool canConstruct(string ransomNote, string magazine) {
    vector<int> ans(26, 0);
    for (char c : magazine)
        ++ans[c - 'a'];
    for (char c : ransomNote)
        --ans[c - 'a'];
    // 和242相比就返回条件稍作修改
    for (int i : ans)
        if (i < 0)
            return false;
    return true;
}