# Code language: Python classSolution: defcountConsistentStrings(self, allowed: str, words: List[str]) -> int: returnsum(all(s in st for s in w) for w in words) if (st := set(allowed)) else0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// Code language: Java classSolution { publicintcountConsistentStrings(String allowed, String[] words) { intcnt=0; boolean[] st = newboolean[27]; for (inti=0, n = allowed.length(); i < n; ++i) st[allowed.charAt(i) - 'a'] = true; out: for (String w: words) { for (inti=0, n = w.length(); i < n; ++i) { if (!st[w.charAt(i) - 'a']) continue out; } ++cnt; } return cnt; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Code language: C++ classSolution { public: intcountConsistentStrings(string allowed, vector<string>& words){ int cnt = 0; bool st[27]{false}; for (int i = 0, n = allowed.size(); i < n; ++i) st[allowed[i] - 'a'] = true; for (string &w: words) { bool tag = true; for (int i = 0, n = w.size(); i < n; ++i) { if (!st[w[i] - 'a']) tag = false; if (!tag) break; } if (tag) ++cnt; } return cnt; } };
/* Code language: JavaScript */ /** * @param {string} allowed * @param {string[]} words * @return {number} */ var countConsistentStrings = function(allowed, words) { const st = newMap(); let cnt = 0; for (let s of allowed) st[s] = true; for (let w of words) { let tag = true; for (let s of w) { if (!st[s]) tag = false; if (!tag) break; } if (tag) cnt++; } return cnt; };