题解 | #农场智能管理系统#

农场智能管理系统

https://www.nowcoder.com/practice/a1461ace00224e5a9fc6ff3d1ae587e5

题目考察的知识点:字符串

题目解答方法的文字分析:用一个数组统计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";
    }
};

全部评论

相关推荐

投递阿里巴巴控股集团等公司7个岗位 >
点赞 评论 收藏
转发
1 1 评论
分享
牛客网
牛客企业服务