From a4871df28e71a638207d926c2e73794dbcd2f8e0 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Tue, 10 Dec 2024 23:49:05 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB2981.=20Find=20Longest=20Spec?= =?UTF-8?q?ial=20Substring=20That=20Occurs=20Thrice=20I=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i/ Signed-off-by: Matej Focko --- ...-special-substring-that-occurs-thrice-i.cs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 cs/find-longest-special-substring-that-occurs-thrice-i.cs diff --git a/cs/find-longest-special-substring-that-occurs-thrice-i.cs b/cs/find-longest-special-substring-that-occurs-thrice-i.cs new file mode 100644 index 0000000..4dd859f --- /dev/null +++ b/cs/find-longest-special-substring-that-occurs-thrice-i.cs @@ -0,0 +1,26 @@ +public class Solution { + public int MaximumLength(string s) { + var counter = new Dictionary<(char, int), int>(); + + for (var l = 0; l < s.Length; ++l) { + var c = s[l]; + + var substringLength = 0; + for (var r = l; r < s.Length && c == s[r]; ++r) { + ++substringLength; + + var key = (c, substringLength); + counter[key] = 1 + (counter.ContainsKey(key) ? counter[key] : 0); + } + } + + return counter.Select(pair => { + if (pair.Value < 3) { + return null; + } + + var (c, length) = pair.Key; + return (int?)length; + }).Max() ?? -1; + } +}