Skip to content

Instantly share code, notes, and snippets.

@Ilchert
Created January 11, 2024 09:30
Show Gist options
  • Save Ilchert/1a504a709a198f53b08e456ba257e677 to your computer and use it in GitHub Desktop.
Save Ilchert/1a504a709a198f53b08e456ba257e677 to your computer and use it in GitHub Desktop.
//solution2("...xxx..x....xxx.", 7);
//solution("aaAbcCABBc");
static int solution2(string S, int B)
{
if (B < 2 || S.Length == 0)
return 0;
var potholes = new List<int>();
var count = 1;
for (int i = 0; i < S.Length - 1; i++)
{
if (S[i] == 'x' && S[i + 1] == 'x')
count++;
else if (S[i] == 'x')
{
potholes.Add(count);
count = 1;
}
}
if (S[S.Length - 1] == 'x')
{
potholes.Add(count);
}
potholes.Sort();
var fixedCount = 0;
for (var i = potholes.Count - 1; i >= 0; i--)
{
if (potholes[i] < B)
{
fixedCount += potholes[i];
B -= potholes[i] + 1;
}
else if (B > 1)
{
fixedCount += B - 1;
break;
}
else if (B <= 1)
break;
}
return fixedCount;
}
static int solution(string letters)
{
if (letters.Length == 0)
return 0;
var lower = new Dictionary<char, int>();
var upper = new Dictionary<char, int>();
for (int i = 0; i < letters.Length; i++)
{
if (char.IsLower(letters[i]))
lower[letters[i]] = i;
}
for (var i = letters.Length - 1; i >= 0; i--)
{
if (char.IsUpper(letters[i]))
upper[letters[i]] = i;
}
var count = 0;
foreach (var lowerItem in lower)
{
if (upper.TryGetValue(char.ToUpper(lowerItem.Key), out var upperIndex))
if (upperIndex > lowerItem.Value)
count++;
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment