当前位置: 代码迷 >> 综合 >> [算法与数据结构][array operations][leetcode1160:easy]1160. Find Words That Can Be Formed by Characters
  详细解决方案

[算法与数据结构][array operations][leetcode1160:easy]1160. Find Words That Can Be Formed by Characters

热度:52   发布时间:2024-02-13 01:46:08.0

直方图的思想
static, const的变量

class Solution {
public:int countCharacters(const vector<string>& words, const string& chars) {int ret = 0;auto his = cntFreq(chars);for (auto& w : words)if (equal(his, cntFreq(w)))ret += w.size();return ret;}
private:inline vector<int> cntFreq(const string& s) {vector<int> cnt_freq(26, 0);for (char c : s)cnt_freq[c - 'a']++;return cnt_freq;}inline bool equal(const vector<int>& a, const vector<int>& b) {for (int i = 0; i < a.size(); i++)if (a[i] < b[i])return false;return true;}
};
static const auto speedup = []() {std::ios::sync_with_stdio(false); std::cin.tie(nullptr); return 0;
}();```
  相关解决方案