本题我并没有想到要用哈希表标记数组的做法,这说明我对哈希表,标记数组这一块的知识点掌握的并不是很熟练,这道题让我们在words数组中的每个元素的每个字符在allowed数组中是否出现。
我们先定义一个哈希表,其中的元素都为0.
我们先将allowd数组中的元素都标记为1,表示出现过,然后遍历words数组的每个元素,然后再遍历一遍words数组中每个元素的每个字符,如果发现这个字符在哈希表中没有标记成1,代表没有出现过,这时候我们break跳出循环。
反思:哈希表的运用不是很熟练。
class Solution {
public:
int countConsistentStrings(string allowed, vector<string>& words) {
int len=allowed.size();
int n=words.size();
int count=0;
int hash[1000]={0};
for(int i=0;i<len;i++){
hash[allowed[i]]=1;
}
for(int i=0;i<n;i++){
bool ret=true;
for(int j=0;j<words[i].size();j++){
char c=words[i][j];
if(hash[c]==0){
ret=false;
break;
}
}
if(ret==true){
count++;
}
}
return count;
}
};
