Solution Code
C#
public class Solution {
public int MarkCharacters(String word) {
int marked = 0;
foreach (char ch in word.ToCharArray()) {
int position = ch - 'a';
marked |= 1 << position;
}
return marked;
}
public int CountConsistentStrings(string allowed, string[] words) {
int markedAllowed = MarkCharacters(allowed);
int answer = 0;
foreach (String word in words) {
int markedWord = MarkCharacters(word);
if ((markedWord & markedAllowed) == markedWord)
answer++;
}
return answer;
}
}
C++
class Solution {
public:
int markCharacters(string &word) {
int marked = 0;
for (char ch : word) {
int position = ch - 'a';
marked |= 1 << position;
}
return marked;
}
int countConsistentStrings(string allowed, vector<string>& words) {
int markedAllowed = markCharacters(allowed);
int answer = 0;
for (string word : words) {
int markedWord = markCharacters(word);
if ((markedWord & markedAllowed) == markedWord)
answer++;
}
return answer;
}
};
Java
class Solution {
public int markCharacters(String word) {
int marked = 0;
for (char ch : word.toCharArray()) {
int position = Character.getNumericValue(ch);
marked |= 1 << position;
}
return marked;
}
public int countConsistentStrings(String allowed, String[] words) {
int markedAllowed = markCharacters(allowed);
int answer = 0;
for (String word : words) {
int markedWord = markCharacters(word);
if ((markedWord & markedAllowed) == markedWord)
answer++;
}
return answer;
}
}