LeetCode 1160. 拼写单词

题目传送门
简单的哈希。

class Solution {
     
public:
	int countCharacters(vector<string>& words, string chars) {
     
		int res = 0;
		int ch[26] = {
      0 };
		for (char c : chars) {
     
			++ch[c - 'a'];
		}
		for (string str : words) {
     
			int wd[26] = {
      0 };
			res += str.size();
			for (char c : str) {
     
				if (++wd[c - 'a'] > ch[c - 'a']) {
     
					res -= str.size();
					break;
				}
			}

		}
		return res;
	}
};

你可能感兴趣的:(LeetCode)