Skip to content

Instantly share code, notes, and snippets.

@unilecs
Last active March 17, 2024 01:57
Show Gist options
  • Save unilecs/ae7be8ad8c996e00cc69aa3cae9f303f to your computer and use it in GitHub Desktop.
Save unilecs/ae7be8ad8c996e00cc69aa3cae9f303f to your computer and use it in GitHub Desktop.
Задача: наибольшая подстрока между двумя одинаковыми символами
using System;
using System.Collections.Generic;
public class Program
{
public static int FindMaxLengthBetweenEqualCharacters(string s) {
int maxLen = -1;
var map = new Dictionary<char, int>();
for (int i = 0; i < s.Length; i++)
{
if (!map.ContainsKey(s[i]))
{
map[s[i]] = i;
}
else
{
maxLen = Math.Max(maxLen, i - map[s[i]] - 1);
}
}
return maxLen;
}
public static void Main()
{
Console.WriteLine("UniLecs");
// tests
Console.WriteLine(FindMaxLengthBetweenEqualCharacters("abca").ToString()); // 2
Console.WriteLine(FindMaxLengthBetweenEqualCharacters("abcaefghqwrta").ToString()); // 11
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment