cs: add «1684. Count the Number of Consistent Strings»

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-09-12 11:09:22 +02:00
parent 6dbeb7ce10
commit d3d901d5e7
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,19 @@
public class Solution {
private static int GetMask(string allowed) {
var mask = 0;
foreach (var c in allowed) {
mask |= 1 << (c - 'a');
}
return mask;
}
private static bool IsConsistent(int mask, string word)
=> word.All(c => (mask & (1 << (c - 'a'))) != 0);
public int CountConsistentStrings(string allowed, string[] words) {
var mask = GetMask(allowed);
return words.Count(word => IsConsistent(mask, word));
}
}