[C++] Linear Fastest Solution, 100% Time, 100% Space
Nice first problem of the day, to solve it we just need a couple of variables:\n res, our result variable, that we set equal to the length of words - ie: we ass
Nice first problem of the day, to solve it we just need a couple of variables:\n* `res`, our result variable, that we set equal to the length of `words` - ie: we assume initially all the words are valid (bear with me!);\n* `alpha`, an array of 26 booean characters, initially all set to `false` and that we will populate flagging to `true` the cell matching the characters in `allowed`, normalised to be in the `0 - 25` range by subtracting `\'a\'`.\n\nSo, for exampe if you had `allowed == "bef"`, you would have `alpha` as:\n\n```cpp\n0 1 0 0 1 1 0 0 0 0... // all the other values are 0\na b c d e f g h j k... // matching character values added for ease of reading\n```\n\nThen we loop throught each `word` in `words`, and then through each character `c` in each `word` and as soon as we find a non matching character (`!alpha[c - \'a\']`), we decrease `res` (one less valid word) and `break`.\n\nFinally, we can return `res` :)\n\nThe code:\n\n```cpp\nclass Solution {\npublic:\n    int countConsistentStrings(string allowed, vector<string>& words) {\n        // support variable\n        int res = words.size();\n        bool alpha[26] = {};\n        // populating alpha\n        for (char c: allowed) alpha[c - \'a\'] = true;\n        // parsing all the words to see if each character is in alpha\n        for (string word: words) {\n            // in case we find an unallowed character, we decrease res and break\n            for (char c: word) if (!alpha[c - \'a\']) {\n                res--;\n                break;\n            }\n        }\n        return res;\n    }\n};\n```\n\nAlternative version burning a few more bytes (not really those many with booleans) to avoid having to compute `c - \'a\'` too many times, which went surprisingly much faster:\n\n```cpp\nclass Solution {\npublic:\n    int countConsistentStrings(string allowed, vector<string>& words) {\n        // support variable\n        int res = words.size();\n        bool alpha[123] = {};\n        // populating alpha\n        for (char c: allowed) alpha[c] = true;\n        // parsing all the words to see if each character is in alpha\n        for (string word: words) {\n            // in case we find an unallowed character, we decrease res and break\n            for (char c: word) if (!alpha[c]) {\n                res--;\n                break;\n            }\n        }\n        return res;\n    }\n};\n```\n\n![image](https://assets.leetcode.com/users/images/ef34f521-4a4b-4531-8a47-b301f99d863a_1607811891.3002427.png)\n
['C', 'C++']