Day i do not know

This commit is contained in:
Electrominch
2022-10-18 00:45:31 +03:00
parent 01c5720116
commit 284b6ff10c
72 changed files with 1256 additions and 12 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_424._Longest_Repeating_Character_Replacement</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,9 @@
namespace _424._Longest_Repeating_Character_Replacement;
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Solution().CharacterReplacement("ABAB", 2));
}
}

View File

@@ -0,0 +1,56 @@
namespace _424._Longest_Repeating_Character_Replacement;
public class Solution
{
public int CharacterReplacement(string s, int k)
{
List<Counter> counters = new List<Counter>();
int max = 0;
for (int i = 0; i < s.Length; i++)
{
char ch = s[i];
for (int cIndex = 0; cIndex < counters.Count; cIndex++)
{
Counter curC = counters[cIndex];
curC.Add(ch);
if (curC.IsValid == false)
{
max = Math.Max(max, curC.ValidLength);
counters.RemoveAt(cIndex);
cIndex--;
}
}
counters.Add(new Counter(ch, k));
}
foreach (Counter curC in counters)
max = Math.Max(max, curC.ValidLength);
return max;
}
private class Counter
{
public int Length { get; private set; }
public bool IsValid => _maxMisses >= Length- _maxCount;
public int ValidLength => _maxCount + Math.Min(Length- _maxCount, _maxMisses);
private int[] _letters = new int[26];
private int _maxCount => _letters[_maxLetter-65];
private char _maxLetter;
private readonly int _maxMisses;
public Counter(char ch, int k)
{
_maxMisses = k;
Length = 1;
_letters[ch - 65] = 1;
_maxLetter = ch;
}
public void Add(char ch)
{
_letters[ch - 65]+=1;
if (_letters[ch - 65] > _maxCount)
_maxLetter = ch;
Length++;
}
}
}