Longest Substring with At Most Two Distinct Characters

Given a string, find the length of the longest substring T that contains at most 2 distinct characters.

For example, Given s = “eceba”,

T is “ece” which its length is 3.

namespace ConsoleApplication3
{
    using System;
    using System.Collections.Generic;
    class Program
    {
        private static string maxSubStrWithNChars(string str, int n)
        {
            if (n < 1)
                return "";
            int s = 0, e = 0, maxS = 0, maxLen = 0;
            Dictionary<char, int> dic = new Dictionary<char, int>();
            while (s <= e && e < str.Length)
            {
                while (e < str.Length && dic.Count <= n)
                {
                    if (!dic.ContainsKey(str[e]))
                    {
                        dic.Add(str[e], 1);
                    }
                    else
                    {
                        ++dic[str[e]];
                    }

                    if (e - s + 1 > maxLen && dic.Count <= n)
                    {
                        maxLen = e - s + 1;
                        maxS = s;
                    }

                    ++e;
                }
                while (s <= e && dic.Count > n)
                {
                    if (--dic[str[s]] == 0)
                    {
                        dic.Remove(str[s]);
                    }
                    ++s;
                }
            }
            return str.Substring(maxS, maxLen);
        }
        static void Main(string[] args)
        {
            string str1 = "";                //expect: 0 ""
            string str2 = "a";                //expect: 1 "a"
            string str3 = "aa";                //expect: 2 "aa"
            string str4 = "aba";            //expect: 3 "aba"
            string str5 = "abcd";            //expect: 2 "ab"
            string str6 = "abcdedcba";        //expect: 3 "ded"
            string str7 = "abbcdededcba";    //expect: 5 "deded"
            string str8 = "eceba";            //expect: 3 "ece"
            string str9 = "abaece";            //expect: 3 "aba"
            string str10 = "ababcd";        //expect: 4 "abab"
            string str11 = "cababcd";        //expect: 4 "abab"
            string str12 = "abcdefgabcdefg";//expect: 2 "ab"
            string str13 = "ababababababab";//expect: 14 "ababababababab"

            Console.WriteLine(maxSubStrWithNChars(str1, 2));
            Console.WriteLine(maxSubStrWithNChars(str2, 2));
            Console.WriteLine(maxSubStrWithNChars(str3, 2));
            Console.WriteLine(maxSubStrWithNChars(str4, 2));
            Console.WriteLine(maxSubStrWithNChars(str5, 2));
            Console.WriteLine(maxSubStrWithNChars(str6, 2));
            Console.WriteLine(maxSubStrWithNChars(str7, 2));
            Console.WriteLine(maxSubStrWithNChars(str8, 2));
            Console.WriteLine(maxSubStrWithNChars(str9, 2));
            Console.WriteLine(maxSubStrWithNChars(str10, 2));
            Console.WriteLine(maxSubStrWithNChars(str11, 2));
            Console.WriteLine(maxSubStrWithNChars(str12, 2));
            Console.WriteLine(maxSubStrWithNChars(str13, 2));
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值