From 8171cd6289900c0d7fa1bfa9b82fb2bde1426c30 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Sun, 7 Jan 2024 13:49:00 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=E2=80=9C1657.=20Determine=20if=20T?= =?UTF-8?q?wo=20Strings=20Are=20Close=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cs/determine-if-two-strings-are-close.cs | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 cs/determine-if-two-strings-are-close.cs diff --git a/cs/determine-if-two-strings-are-close.cs b/cs/determine-if-two-strings-are-close.cs new file mode 100644 index 0000000..e77f723 --- /dev/null +++ b/cs/determine-if-two-strings-are-close.cs @@ -0,0 +1,33 @@ +public class Solution { + private static List Freqs(string word) { + var f = new List(Enumerable.Repeat(0, 26)); + + foreach (var c in word) { + ++f[c - 'a']; + } + + return f; + } + + private static int Mask(List f) { + var m = 0; + + foreach (var c in f) { + m = (m << 1) | (c > 0 ? 1 : 0); + } + + return m; + } + + public bool CloseStrings(string word1, string word2) { + var f1 = Freqs(word1); + var m1 = Mask(f1); + f1.Sort(); + + var f2 = Freqs(word2); + var m2 = Mask(f2); + f2.Sort(); + + return m1 == m2 && f1.SequenceEqual(f2); + } +}