> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Count the Number of Consistent Strings

> Tested Python solution for LeetCode 1684 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 1684, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [String](/catalog/topics/string), [Bit Manipulation](/catalog/topics/bit-manipulation), [Counting](/catalog/topics/counting). [View on LeetCode](https://leetcode.com/problems/count-the-number-of-consistent-strings/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 1684   # by problem number
lcpy gen -s count_the_number_of_consistent_strings   # by problem name
```

## Problem

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`.

### Examples

```
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'.
```

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

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

### Constraints

* 1 \<= words.length \<= 10^4
* 1 \<= allowed.length \<= 26
* 1 \<= words\[i].length \<= 10
* The characters in allowed are distinct.
* words\[i] and allowed contain only lowercase English letters.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_the_number_of_consistent_strings/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/count_the_number_of_consistent_strings/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n * m) where n = len(words), m = max word length
    # Space: O(1) since allowed is at most 26 characters
    def count_consistent_strings(self, allowed: str, words: list[str]) -> int:
        allowed_mask = 0
        for ch in allowed:
            allowed_mask |= 1 << (ord(ch) - ord("a"))

        count = 0
        for word in words:
            word_mask = 0
            for ch in word:
                word_mask |= 1 << (ord(ch) - ord("a"))
            if word_mask | allowed_mask == allowed_mask:
                count += 1
        return count
```

## Complexity

| Time | Space |
| - | - |
| O(n \* m) where n = len(words), m = max word length | O(1) since allowed is at most 26 characters |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.