defcountConsistentStrings(allowed: str, words: list[str]) -> int: mask = 0 for c in allowed: mask |= 1 << (ord(c) - ord('a')) res = 0 for word in words: mask1 = 0 for c in word: mask1 |= 1 << (ord(c) - ord('a')) res += (mask | mask1) == mask return res
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
classSolution { public: intcountConsistentStrings(string allowed, vector<string>& words){ int mask = 0; for (auto c: allowed) { mask |= 1 << (c - 'a'); } int res = 0; for (auto word: words) { int mask1 = 0; for (auto c: word) { mask1 |= 1 << (c - 'a'); } if ((mask | mask1) == mask) ++res; } return res; } };