public static int CalcNoRepeatCount(string characterStr) { int length = characterStr.Length; int currentCount = 0; int maxCount = 0; int left = 0; List<string> cacheList = new List<string>(); List<string> maxList = new List<string>(); for (int i = 0; i < length; i++) { currentCount += 1; while (cacheList.Contains(characterStr.Substring(i, 1))) { cacheList.Remove(characterStr.Substring(left, 1)); left += 1; currentCount -= 1; } cacheList.Add(characterStr.Substring(i, 1)); if (currentCount > maxCount) { maxCount = currentCount; maxList.Clear(); foreach (var item in cacheList) { maxList.Add(item); } } } string substr = ""; foreach (var item in maxList) { substr += item; } Console.WriteLine(substr); return maxCount; }