题目
给你一份『词汇表』(字符串数组) words 和一张『字母表』(字符串) chars。
假如你可以用 chars 中的『字母』(字符)拼写出 words 中的某个『单词』(字符串),那么我们就认为你掌握了这个单词。
注意:每次拼写(指拼写词汇表中的一个单词)时,chars 中的每个字母都只能用一次。
返回词汇表 words 中你掌握的所有单词的 长度之和。
示例 1:
输入:words = ["cat","bt","hat","tree"], chars = "atach"
输出:6
解释:
可以形成字符串 "cat" 和 "hat",所以答案是 3 + 3 = 6。
示例 2:
输入:words = ["hello","world","leetcode"], chars = "welldonehoneyr"
输出:10
解释:
可以形成字符串 "hello" 和 "world",所以答案是 5 + 5 = 10。
提示:
1 <= words.length <= 1000
1 <= words[i].length, chars.length <= 100
所有字符串中都仅包含小写英文字母
算法
-
一次遍历chars,一次遍历words,24ms的99.73%,8.2MB的100%
-
围绕hash表的理念来解,将chars生成一个hash表,一共26个int空间对应26个字母,不出现的为0,出现的为1,重复则++
-
重点:hash表要保留着让所有word都能用来查找,因此创建一个同等大小的count计数数组
-
遍历单个word过程中,长度超过chars长度的直接跳过;
-
判断字符在hash表中是否出现(hash值不为0),如果出现重复,判断count值是否超过hash,超过说明该字母在chars中的重复个数不足
-
如果判断没问题,count值+1;如果循环中无break,max加上该word的长度.一轮word遍历后,下一轮word遍历前,count必须置零!
-
其他的细节在下面代码中有讲解。希望有人能把这一点点0.027的时间差优化,时间效率上提到100%
代码
int countCharacters(char ** words, int wordsSize, char * chars){
int nums = strlen(chars);
if(wordsSize == 0||nums == 0) return 0; //一者为空直接出局
int max = 0, j = 0;
int map[26], count[26]; //hash表与对应的计数数组
memset(map, 0, sizeof(map)); //hash表初始化
for(int c = 0;c < nums;++c){ //生成hash值
map[chars[c] - 'a'] ++;
}
for(int i = 0;i < wordsSize;++i){
int len = strlen(words[i]);
if(len > nums) continue; //单个words长度超过字母表长度, 无效
memset(count, 0, sizeof(count)); //计数数组归0
for(j = 0; j<len;++j){
if(map[words[i][j] - 'a'] <= count[words[i][j] - 'a']) break; //<, hash值为0, map中不存在这个字符或 =, 已经全部用完了
count[words[i][j] - 'a']++;
}
if(j==len) max += len; //当words中一个字符串全部匹配上时, 才能加上这个长度
}
return max;
}