1题目:
给你一个由不同字符组成的字符串 allowed 和一个字符串数组 words 。如果一个字符串的每一个字符都在 allowed 中,就称这个字符串是 一致字符串 。
请你返回 words 数组中 一致字符串 的数目。
测试用例:
输入:allowed = "ab", words = ["ad","bd","aaab","baa","badab"]
输出:2
解释:字符串 "aaab" 和 "baa" 都是一致字符串,因为它们只包含字符 'a' 和 'b' 。
题解如下:
int countConsistentStrings(char * allowed, char ** words, int wordsSize){
int ret = 0,count;
for(int i = 0; i < wordsSize ; i++)
{
count = 0;
char *word = words[i];//将每个字符串传给word
for(int j = 0 ; j < strlen(word) ; j++)
{
for(int k = 0 ; k < strlen(allowed) ; k++)
{
if(word[j] == allowed[k])
count++; //同时对比allowed和word里的字符
}
if(count == strlen(word))
ret++;
}
}
return ret;
}
思路:
通过遍历两个字符串,来判断两个字符是否相等。