Day i do not know
This commit is contained in:
@@ -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>
|
||||
9
424. Longest Repeating Character Replacement/Program.cs
Normal file
9
424. Longest Repeating Character Replacement/Program.cs
Normal 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));
|
||||
}
|
||||
}
|
||||
56
424. Longest Repeating Character Replacement/Solution.cs
Normal file
56
424. Longest Repeating Character Replacement/Solution.cs
Normal 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++;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user