1160.拼写单词
链接:1160.拼写单词
难度:Easy
标签:数组、哈希表、字符串
简介:返回词汇表 words 中你掌握的所有单词的 长度之和。
题解 1 - cpp
- 编辑时间:2022-03-29
- 执行用时:40ms
- 内存消耗:14.9MB
- 编程语言:cpp
- 解法介绍:哈希存储。
class Solution {
   public:
    int countCharacters(vector<string> &words, string chars) {
        int list[26] = {0}, ans = 0, tmp[26] = {0};
        for (auto &ch : chars) list[ch - 'a']++;
        for (auto &word : words) {
            memset(tmp, 0, sizeof(int) * 26);
            int f = 1;
            for (auto &ch : word) tmp[ch - 'a']++;
            for (int i = 0; i < 26; i++) {
                if (list[i] < tmp[i]) {
                    f = 0;
                    break;
                }
            }
            if (f) ans += word.size();
        }
        return ans;
    }
};