classSolution: defcountConsistentStrings(self, 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 += (mask1 | mask) == mask return res
classSolution { publicintcountConsistentStrings(String allowed, String[] words) { intmask=0; for (inti=0; i < allowed.length(); i++) { charc= allowed.charAt(i); mask |= 1 << (c - 'a'); } intres=0; for (String word : words) { intmask1=0; for (inti=0; i < word.length(); i++) { charc= word.charAt(i); mask1 |= 1 << (c - 'a'); } if ((mask1 | mask) == mask) { res++; } } return res; } }
[sol1-C#]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassSolution { publicintCountConsistentStrings(string allowed, string[] words) { int mask = 0; foreach (char c in allowed) { mask |= 1 << (c - 'a'); } int res = 0; foreach (string word in words) { int mask1 = 0; foreach (char c in word) { mask1 |= 1 << (c - 'a'); } if ((mask1 | mask) == mask) { res++; } } return res; } }
[sol1-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
intcountConsistentStrings(char * allowed, char ** words, int wordsSize){ int mask = 0; for (int i = 0; allowed[i] != '\0'; i++) { mask |= 1 << (allowed[i] - 'a'); } int res = 0; for (int i = 0; i <wordsSize; i++) { int mask1 = 0; for (int j = 0; words[i][j] != '\0'; j++) { mask1 |= 1 << (words[i][j] - 'a'); } if ((mask1 | mask) == mask) { res++; } } return res; }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
funccountConsistentStrings(allowed string, words []string) (res int) { mask := 0 for _, c := range allowed { mask |= 1 << (c - 'a') } for _, word := range words { mask1 := 0 for _, c := range word { mask1 |= 1 << (c - 'a') } if mask1|mask == mask { res++ } } return }
[sol1-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
var countConsistentStrings = function(allowed, words) { let mask = 0; for (let i = 0; i < allowed.length; i++) { const c = allowed[i]; mask |= 1 << (c.charCodeAt() - 'a'.charCodeAt()); } let res = 0; for (const word of words) { let mask1 = 0; for (let i = 0; i < word.length; i++) { const c = word[i]; mask1 |= 1 << (c.charCodeAt() - 'a'.charCodeAt()); } if ((mask1 | mask) === mask) { res++; } } return res; };