cs: add «2981. Find Longest Special Substring That Occurs Thrice I»
URL: https://leetcode.com/problems/find-longest-special-substring-that-occurs-thrice-i/ Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
parent
aa53335202
commit
a4871df28e
1 changed files with 26 additions and 0 deletions
26
cs/find-longest-special-substring-that-occurs-thrice-i.cs
Normal file
26
cs/find-longest-special-substring-that-occurs-thrice-i.cs
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue