---
# System prepended metadata

title: Leetcode 1684. Count the Number of Consistent Strings
tags: [Leetcode]

---

# Leetcode 1684. Count the Number of Consistent Strings
###### tags: `Leetcode` 

題目 
You are given a string allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed.

Return the number of consistent strings in the array words.

 

Example 1:

Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"]
Output: 2
Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'.
Example 2:

Input: allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]
Output: 7
Explanation: All strings are consistent.
Example 3:

Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]
Output: 4
Explanation: Strings "cc", "acd", "ac", and "d" are consistent.

解法:
1.for(int i =0; words[][]; i++)=>這樣可以遍歷字串
2.當作有26個格子，讓allowed裡面對應英文數字的格子變成1
3.遍歷字串，然後比較某個字串的格子跟一開始allowed的格子是否一樣if(allowedbit == (allowedbit|s))

＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝＝

```


int countConsistentStrings(char * allowed, char ** words, int wordsSize){
    int allowedbit = 0;
    int count = 0;
    int index = 0;
    while(allowed[index] != '\0')
    {
        allowedbit |= 1<<(allowed[index]-'a');
        index++;
    }
    
    for(int i = 0; i < wordsSize; i++)
    {
        int s = 0;
        for(int j = 0; words[i][j]; j++)
        {
            s|=1<<(words[i][j]-'a');
        }
        if(allowedbit == (allowedbit|s))
            count++;
        
    }
    return count;
}
```