Count the Number of Consistent Strings

easy bitmask string

Problem

Given an allowed string of distinct lowercase letters and a list of words, count how many words consist only of allowed letters.

Inputallowed = "ab", words = ["ad","bd","aaab","baa","badab"]
Output2
Only "aaab" and "baa" contain only a,b.

def count_consistent_strings(allowed, words):
    mask = 0
    for c in allowed:
        mask |= 1 << (ord(c) - ord('a'))
    cnt = 0
    for w in words:
        wmask = 0
        for c in w:
            wmask |= 1 << (ord(c) - ord('a'))
        if (wmask & ~mask) == 0:
            cnt += 1
    return cnt
function countConsistentStrings(allowed, words) {
  let mask = 0;
  for (const c of allowed) mask |= 1 << (c.charCodeAt(0) - 97);
  let cnt = 0;
  for (const w of words) {
    let wm = 0;
    for (const c of w) wm |= 1 << (c.charCodeAt(0) - 97);
    if ((wm & ~mask) === 0) cnt++;
  }
  return cnt;
}
class Solution {
    public int countConsistentStrings(String allowed, String[] words) {
        int mask = 0;
        for (char c : allowed.toCharArray()) mask |= 1 << (c - 'a');
        int cnt = 0;
        for (String w : words) {
            int wm = 0;
            for (char c : w.toCharArray()) wm |= 1 << (c - 'a');
            if ((wm & ~mask) == 0) cnt++;
        }
        return cnt;
    }
}
int countConsistentStrings(string allowed, vector<string>& words) {
    int mask = 0;
    for (char c : allowed) mask |= 1 << (c - 'a');
    int cnt = 0;
    for (auto& w : words) {
        int wm = 0;
        for (char c : w) wm |= 1 << (c - 'a');
        if ((wm & ~mask) == 0) cnt++;
    }
    return cnt;
}
Time: O(total chars) Space: O(1)