From d3d901d5e7a295c61254d30ea76c5dbd190f439f Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 12 Sep 2024 11:09:22 +0200 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB1684.=20Count=20the=20Number?= =?UTF-8?q?=20of=20Consistent=20Strings=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/count-the-number-of-consistent-strings.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 cs/count-the-number-of-consistent-strings.cs diff --git a/cs/count-the-number-of-consistent-strings.cs b/cs/count-the-number-of-consistent-strings.cs new file mode 100644 index 0000000..26f375e --- /dev/null +++ b/cs/count-the-number-of-consistent-strings.cs @@ -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)); + } +}