给你一个由不同字符组成的字符串 allowed
和一个字符串数组 words
。如果一个字符串的每一个字符都在 allowed
中,就称这个字符串是 一致字符串 。
请你返回 words
数组中 一致字符串 的数目。
class Solution {
public:
int countConsistentStrings(string allowed, vector<string>& words) {
//一致字符串的数目
int count=0;
//标记allowed中字母的出现次数
int a[26]={0};
//遍历allowed,统计出现次数
for(int i=0;i<allowed.size();i++){
a[allowed[i]-'a']++;
}
//遍历words
for(int j=0;j<words.size();j++){
//定义布尔型 初始化为true表示是一致字符串
bool iscount=true;
//遍历words中单词的字母
for(int k=0;k<words[j].size();k++){
//如果字符串的字母相对a的偏移在a数组中为0,表示未在allowed中出现
if(a[words[j][k]-'a']==0){
//不是一致字符串,并退出循环
iscount=false;
break;
}
}
//如果是一致字符串,count++
if(iscount){
count++;
}
}
//返回一致字符串的数量
return count;
}
};