Simple and easy C++ Code || ☠️💯✅
Code\ncpp []\nclass Solution {\npublic:\n    int countConsistentStrings(string allowed, vector<string>& words) {\n        set<char> ans;\n        for (auto it :
# Code\n```cpp []\nclass Solution {\npublic:\n    int countConsistentStrings(string allowed, vector<string>& words) {\n        set<char> ans;\n        for (auto it : allowed) {\n            ans.insert(it);\n        }\n        int count = 0;\n        for (int i = 0; i < words.size(); i++) {\n            bool f1 = true;\n            for (auto it : words[i]) {\n                if (ans.find(it) == ans.end()) {\n                    f1 = false;\n                    break;\n                }\n            }\n            if (f1) {\n                count++;\n            }\n        }\n        return count;\n    }\n};\n\n```\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem you\'re tackling is to count how many strings in a list (words) consist only of characters from a given string (allowed). Let\'s break down the approach step by step, including the intuition behind it.\n# Problem Overview\n**We are given:**\n\nA string `allowed`, which contains a set of characters that are considered "valid."\nA list of strings `words`, where we need to count how many of these strings are composed only of the characters present in `allowed`.\n# Goal\nWe need to determine how many strings in the `words` list are "consistent" \u2014 meaning that each string must consist solely of characters found in the `allowed` string.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. **Storing allowed Characters in a Set**\n**Intuition:** \nWe want to quickly check if each character in a string is part of the `allowed` characters. A `set` data structure is ideal for this because checking membership in a set is on average O(1) time complexity.\n\n   **Explanation:** We loop through the characters of `allowed `and insert each character into a `set<char>` called `ans`. This gives us an easy and fast way to check if any character belongs to allowed.\n```\nset<char> ans;\nfor (auto it : allowed) {\n    ans.insert(it);\n}\n```\nAfter this step, ans contains all unique characters from allowed.\n\n2. **Looping Through Each Word**\nIntuition: For each word in the `words` list, we need to check if all its characters are present in the `allowed` set. If even one character is not in `allowed`, that word is not consistent, and we move to the next word.\n\n   **Explanation:** We iterate over each word in `words` using a for loop. For each word, we assume it is "consistent" (set a flag `f1` to `true`). Then, we check each character of the word to see if it\'s in the set ans. If any character is not found in the set, we mark this word as inconsistent (`f1 = false`) and break out of the inner loop to avoid unnecessary checks.\n```\nint count = 0;\nfor (int i = 0; i < words.size(); i++) {\n    bool f1 = true;\n    for (auto it : words[i]) {\n        if (ans.find(it) == ans.end()) {  // Character not in \'allowed\'\n            f1 = false;  // Not consistent\n            break;  // No need to check further characters in this word\n        }\n    }\n    if (f1) {  // If the word is consistent\n        count++;  // Increment the count of consistent strings\n    }\n}\n\n```\n# **Breakdown:**\n\n**Outer loop:** Iterates over each word in the `words` list.\n**Inner loop:** Iterates over each character in the current word.\nIf we find a character that is not in the `allowed` set, the word is marked as inconsistent and we stop further checking for that word (`break` out of the inner loop).\n\n3. **Counting Consistent Strings**\nIf a word is consistent, we increment the count.\n4. **Returning the Result**\nFinally, after processing all the words, we return the count of consistent strings.\n# Complexity\n\n- Inserting characters from `allowed` into the set takes `O(n)`, where `n` is the length of `allowed`.\nFor each word in `words`, checking whether its characters are in the set takes O(m), where `m` is the length of the word. Hence, if the total number of characters in all words combined is `L`, then the time complexity for checking all words is O(L).\nTherefore, the total time complexity is `O(n + L)`, which is efficient.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n# Space complexity:\n- We use a set to store the characters from allowed, which has a space complexity of `O(n)`, where n is the number of unique characters in allowed.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n
['C++']