Longest Substring with At Most Two Distinct Characters
来源:互联网 发布:mysql 集群 编辑:程序博客网 时间:2024/04/28 06:22
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
- [Leetcode] Longest Substring with At Most Two Distinct Characters
- Leetcode Longest Substring with At Most Two Distinct Characters
- leetcode 159: Longest Substring with At Most Two Distinct Characters
- Longest Substring with At Most Two Distinct Characters
- [Leetcode]Longest Substring with At Most Two Distinct Characters
- Longest Substring with At Most Two Distinct Characters
- LeetCode - Longest Substring with At Most Two Distinct Characters
- Leetcode: Longest Substring with At Most Two Distinct Characters
- 159Longest Substring with At Most Two Distinct Characters
- [LeetCode160]Longest Substring with At Most Two Distinct Characters
- Leetcode-159.Longest Substring with At Most Two Distinct Characters
- Leetcode-159.Longest Substring with At Most Two Distinct Characters
- Leetcode 159 Longest Substring with At Most Two Distinct Characters
- [LeetCode]159. Longest Substring with At Most Two Distinct Characters
- 159. Longest Substring with At Most Two Distinct Characters
- LeetCode 159. Longest Substring with At Most Two Distinct Characters
- Longest Substring with At Most Two Distinct Characters
- Longest Substring with At Most Two Distinct Characters
- java学习总结(16.05.07) jdk的安装与环境变量的配置
- memcachd认识汇总
- java学习之final关键字
- CentOS 6.3中安装Gnome桌面环境
- 06 GUI(图形用户界面)
- Longest Substring with At Most Two Distinct Characters
- uva140
- 2016sdau课程练习专题三 1011
- 传统互联网or新兴互联网,向左or向右?
- 347. Top K Frequent Elements
- 关于Arduino Mega2560的最基本介绍
- 特征值和特征向量
- 二进制与十六进制
- android实现代码的动态加载