5 Languages✅ || Simplest Explanation👌👌
Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem requires us to count the number of strings in words that only contain chara
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem requires us to count the number of strings in words that only contain characters from allowed. We can approach this by:\n\nCreating a frequency array: To efficiently check if a character is in allowed.\nIterating through words: For each word, check if all its characters are in allowed.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nInitialize a frequency array: Create an array of size 26 to store the frequency of each lowercase letter in allowed.\nPopulate the frequency array: Iterate through allowed and increment the frequency of each character.\nIterate through words: For each word in words:\nInitialize a flag: Set a flag to true to indicate that the word is consistent.\nIterate through characters in the word: For each character in the word:\nIf the frequency of the character in the frequency array is 0, the word is not consistent. Set the flag to false and break.\nIncrement the count: If the flag is still true after iterating through all characters, increment the count of consistent strings.\nReturn the count: Return the final count of consistent strings.\nComplexity\n\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(M)\n M is the total length characters in  word.\n\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n# Code\n```cpp []\nclass Solution {\npublic:\n    int countConsistentStrings(string allowed, vector<string>& words) {\n        int cnt=0;\n\n     vector<int>freq(26,0);\n        for(auto it:allowed)\n        freq[it-\'a\']++;\n\n        for(auto str:words){\n            cnt++;\n            for(auto ch:str){\n                if(freq[ch-\'a\'] ==0){\n                    cnt--;\n                break;\n                }\n            }\n        }\n\n        return cnt;\n    }\n};\n```\n```c []\n int count = 0;\n    int freq[26] = {0};\n\n    for (int i = 0; allowed[i] != \'\\0\'; i++) {\n        freq[allowed[i] - \'a\']++;\n    }\n\n    for (int i = 0; i < wordsSize; i++) {\n        int j;\n        for (j = 0; words[i][j] != \'\\0\'; j++) {\n            if (freq[words[i][j] - \'a\'] == 0) {\n                break;\n            }\n        }\n\n        if (words[i][j] == \'\\0\') {\n            count++;\n        }\n    }\n\n    return count;\n    }\n};\n```\n```python []\n class Solution(object):\n    def countConsistentStrings(self, allowed, words):\n        freq = [0] * 26\n        for char in allowed:\n            freq[ord(char) - ord(\'a\')] += 1\n\n        count = 0\n        for word in words:\n            is_consistent = True\n            for char in word:\n                if freq[ord(char) - ord(\'a\')] == 0:\n                    is_consistent = False\n                    break\n            if is_consistent:\n                count += 1\n\n        return count\n```\n```java []\nclass Solution {\n    public int countConsistentStrings(String allowed, String[] words) {\n        int count = 0;\n        int[] freq = new int[26];\n\n        // Create a frequency array for allowed characters\n        for (char c : allowed.toCharArray()) {\n            freq[c - \'a\']++;\n        }\n\n        // Iterate through each word\n        for (String word : words) {\n            boolean isConsistent = true;\n            for (char c : word.toCharArray()) {\n                if (freq[c - \'a\'] == 0) {\n                    isConsistent = false;\n                    break;\n                }\n            }\n\n            if (isConsistent) {\n                count++;\n            }\n        }\n\n        return count;\n    }\n}\n```\n```javaScript []\n/**\n * @param {string} allowed\n * @param {string[]} words\n * @return {number}\n */\nvar countConsistentStrings = function(allowed, words) {\n    const freq = new Array(26).fill(0);\n\n    // Create a frequency array for allowed characters\n    for (const char of allowed) {\n        freq[char.charCodeAt(0) - \'a\'.charCodeAt(0)]++;\n    }\n\n    // Iterate through each word\n    let count = 0;\n    for (const word of words) {\n        let isConsistent = true;\n        for (const char of word) {\n            if (freq[char.charCodeAt(0) - \'a\'.charCodeAt(0)] === 0) {\n                isConsistent = false;\n                break;\n            }\n        }\n\n        if (isConsistent) {\n            count++;\n        }\n    }\n\n    return count;\n};\n```\n\n
['Array', 'Hash Table', 'String', 'Ordered Map', 'C', 'Counting', 'Python', 'C++', 'Java', 'JavaScript']