题目考察的知识点:字符串
题目解答方法的文字分析:用一个数组统计allocations中字符出现的次数,然后再减去requirements字符出现的次数,如果有小于0的字符串,则返回no。
本题解析所用的编程语言:c++
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param requirements string字符串 * @param allocations string字符串 * @return string字符串 */ string can_construct(string requirements, string allocations) { // write code here vector<int> v(26); for (auto& e : allocations) ++v[e - 'A']; for (auto& e : requirements) --v[e - 'A']; for (auto& x : v) if (x < 0) return "NO"; return "YES"; } };