[LeetCode] Count the Number of Consistent Strings

1684. Count the Number of Consistent Strings

You are given a string allowed consisting of distinct characters and an array of strings words. A string is consistent if all characters in the string appear in the string allowed.

Return the number of consistent strings in the array words.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
int helper(string& s) {
int res = 0;
for(auto& ch : s) res |= 1ll<<(ch-'a');
return res;
}
public:
int countConsistentStrings(string allowed, vector<string>& words) {
int mask = helper(allowed);
int res = 0;
for(auto& w : words) if((helper(w) | mask) == mask) res++;

return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2024/09/12/PS/LeetCode/count-the-number-of-consistent-strings/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.