Last active
March 17, 2024 01:57
-
-
Save unilecs/ae7be8ad8c996e00cc69aa3cae9f303f to your computer and use it in GitHub Desktop.
Задача: наибольшая подстрока между двумя одинаковыми символами
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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