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

农场智能管理系统

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

知识点

计数

思路

对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";
    }
};

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务