I think your solution will work, but its complexity is too high.
Here is an alternative solution
If counting characters in your string returns {'A', 4}, {'C', 6}, {'G', 6}, {'T', 4}, the substring that must start with C or G ends with C or G and have a length> = 2
So, what we need to do is take each line that checks this condition, check whether it contains “bad characters” in our case, one C and one G. If its length = 2, we win, otherwise we save the temporary variable and continue our test
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; class Solution { static void Main(String[] args) { string[] inputs = { "GAAATAAA", "CACCGCTACCGC", "CAGCTAGC", "AAAAAAAA", "GAAAAAAA", "GATGAATAACCA", "ACGT" }; List<string> replacement = new List<string>(); foreach (var item in inputs) { replacement.Add(StringThatHasToBeReplaced(item)); } } static string StringThatHasToBeReplaced(string s) { var counter = new Dictionary<char, int>() { { 'A', 0 }, { 'C', 0 }, { 'G', 0 }, { 'T', 0 } }; for (int i = 0; i < s.Length; ++i) counter[s[i]] += 1; int div = s.Length / 4; var pairs = counter.ToList(); if (pairs.Where(p => p.Value != div).Count() == 0) { return null; } List<char> surplusCharacter = pairs.Where(p => p.Value > div).Select(p => p.Key).ToList(); int minLength = pairs.Where(p => p.Value > div).Sum(p => p.Value - div); string result = s; for (int i = 0; i < s.Length - minLength + 1; i++)
I made some changes to handle borderline cases. Here are some test cases and the result I get looks great 
source share