Easy Solution | O(n⋅m) | 31ms Beats 97.78% | C++Java Py3
\n---\n# Intuition\nTo determine if a string is consistent, check if all characters in the string are within the allowed set of characters. This can be achieved
#\n---\n# Intuition\nTo determine if a string is consistent, check if all characters in the string are within the allowed set of characters. This can be achieved by iterating through each character of the string and verifying its presence in the allowed set.\n\n# Approach\nUse a set to store allowed characters for quick lookup. For each string in the **words** array, verify if every character of the string is present in the allowed set. Count the number of strings that meet this criterion.\n\n# Complexity\n- **Time Complexity** $$O(n \\cdot m)$$ where **n** is the number of words and **m** is the maximum length of a word. We check each character in each word against the allowed set.\n- **Space Complexity** $$O(k)$$ where **k** is the number of distinct characters in **allowed** (constant space in practical terms).\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n    int countConsistentStrings(string allowed, vector<string>& words) {\n        bool allowedSet[26] = {};\n        for (char c : allowed) allowedSet[c - \'a\'] = true;  \n        int count{0};\n        for (const auto& word : words) \n            count += all_of(word.begin(), word.end(), [&](char c){ return allowedSet[c - \'a\']; });\n        return count;\n    }\n};\n\n```\n```java []\nclass Solution {\n    public int countConsistentStrings(String allowed, String[] words) {\n        boolean[] allowedSet = new boolean[26];\n        for (char c : allowed.toCharArray()) allowedSet[c - \'a\'] = true;\n        int count = 0;\n        for (String word : words) {\n            boolean consistent = true;\n            for (char c : word.toCharArray()) {\n                if (!allowedSet[c - \'a\']) {\n                    consistent = false;\n                    break;\n                }\n            }\n            if (consistent) count++;\n        }\n        return count;\n    }\n}\n\n```\n```python []\nclass Solution:\n    def countConsistentStrings(self, allowed: str, words: List[str]) -> int:\n        allowed_set = set(allowed)\n        return sum(all(char in allowed_set for char in word) for word in words)\n```\n---\n\n- ![image.png](https://assets.leetcode.com/users/images/89555adb-9454-42e8-a23e-efee83c59553_1726107054.9225755.png)\n\n---
['C++', 'Java', 'Python3']