知识点

计数

思路

对allocations和requirements的字母个数进行计数,如果allocations的每个字母都比requirements多则为“YES”

时间复杂度 O(n)

AC Code(C++)

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param requirements string字符串 
     * @param allocations string字符串 
     * @return string字符串
     */
    string can_construct(string requirements, string allocations) {
        vector<int> st(26, 0);
        for (auto x : allocations) {
            st[x - 'A'] += 1;
        }
        for (auto x : requirements) {
            if (-- st[x - 'A'] < 0) return "NO";
        }
        return "YES";
    }
};